约瑟夫JAVA代码 约瑟夫问题vb代码

怎么用java数组实现约瑟夫环用java数组实现约瑟夫环
package Josephround;
public class Joseround {
int sit;
int flagjo=0;
Joseround(){};
Joseround(int x){
sit=x;
}
void setflag(int x){
flagjo=x;
}
}
package Josephround;
public class Inijose {
Joseround jo[];
static int length=0;
Inijose(){};
Inijose(int x){
jo=new Joseround[x];
for(int i=0;ix;i++){
jo[i]=new Joseround(i+1);//创建对象数组
length++;
}
}
void delete(int n){
for(int i=n;ilength-1;i++){
jo[i]=jo[i+1];
}
length--;
}
}
package Josephround;
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
int m,n;
System.out.println("input m");
Scanner m1=new Scanner(System.in);
m=m1.nextInt();
System.out.println("input n");
Scanner n1=new Scanner(System.in);
n=n1.nextInt();
int temp=0;
int x=0;
Inijose joseph=new Inijose(n);
while(joseph.length!=0){
for(int i=1;i=m;i++){
joseph.jo[x].setflag(i);
if(joseph.jo[x].flagjo==m){
System.out.println(joseph.jo[x].sit);
joseph.delete(x);
x--;
}
if(xjoseph.length-1) x++;
else x=0;
}
}
}
}
用java解决约瑟夫问题Java约瑟夫问题: n个人(不同id)围成一个圈 , 从startId(任意数)个开始报数m(任意数)个数 , 数m的人出列排成新队列,m清零 , 然后又从下一个人开始数m个数开始 , 数到m就出列接在新队列尾部,如此重复,知道所有人都出列为止 。
package list;
import java.util.ArrayList;
* 打印 出列后的新队列
*
* eg
* int n = 10;//总人数
* int m = 3;//报数个数
* int startIndex = 1;//起点位置
* @author Hulk201403 20
*
*/
public class JosephListTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
JosephListTest test = new JosephListTest();
int n = 10; //总人数
int m = 3; //报数个数
int startIndex = 12; //起点位置
System.out.println("JosephListTest: n= " + n + ", m= " + m +
", startIndex= " + startIndex + "\n\nQUEUE RESULT:");
ArrayListPerson queueList = test.queuePreson(n, m, startIndex);
for (Person person : queueList) {
System.out.println("OUT person: " + person);
}
System.out.println("use time= " +
(System.currentTimeMillis() - startTime));
}
private ArrayListPerson queuePreson(int n, int m, int startIndex) {
ArrayListPerson queueList = null;
PersonList list = createList(n);
//list.search();
if ((list != null)(list.head != null)) {
queueList = new ArrayListJosephListTest.Person();
PNode pNode = list.head;
if (startIndex0) {
startIndex = startIndex % n;
pNode = list.getNode(startIndex);
} else {
pNode = list.head;
}
int count = 1;
while (list.size0) {
Person outPerson = null;
//find
if (count == (m - 1)) {
//delete next node
Person prev = pNode.person;
outPerson = list.remove(prev);
queueList.add(outPerson);
//System.out.println("OUT person: " + outPerson + ", size= " + list.size);
count = 0;
}
pNode = pNode.next;
count++;
}
}
return queueList;
}
private PersonList createList(int n) {
PersonList list = new PersonList();
for (int i = 0; in; i++) {
Person person = new Person(i, "name_" + (i + 1));
list.add(i, person);
}
return list;
}
public class PersonList {
PNode head = null;
int size = 0;
public PersonList() {

推荐阅读