约瑟夫JAVA代码 约瑟夫问题vb代码( 三 )


}
}
PNode no = head;
for (int i = 0; iindex; i++) {
no = no.next;
}
return no;
}
public void search() {
int sizeLong = size;
PNode no = head;
for (int i = 0; isizeLong; i++) {
System.out.println("search index= " + i + "," + no);
no = no.next;
}
}
}
public class PNode {
Person person;
PNode next = null;
public PNode() {
}
public PNode(Person person) {
this.person = person;
}
public PNode(Person person, PNode next) {
this.person = person;
this.next = next;
}
@Override
public String toString() {
return "PNode [person=" + person.id + ", next=" + next.person.id +
"]";
}
public Person getPerson() {
return person;
}
【约瑟夫JAVA代码 约瑟夫问题vb代码】public void setPerson(Person person) {
this.person = person;
}
public PNode getNext() {
return next;
}
public void setNext(PNode next) {
this.next = next;
}
}
public class Person {
int id = 0;
String name = "";
public Person() {
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
}
java编程序实现约瑟夫环精简易懂版本刚写约瑟夫JAVA代码的
public class Yuesefu {
// 数三退一 约瑟夫环 求最后留在环中小朋友约瑟夫JAVA代码的位置 即原来的编号(1-500)
/**
* 初始化数组,添加标号
*/
private void markID(int[] child) {
for (int i = 0; i = child.length - 1; i++) {
child[i] = i + 1;
}
}
/**
* 求出小孩子原来的位置在什么地方,从一开始
*/
public void re_index(int[] i) {
for (int ii : i) {
if (ii0) {
System.out.println("还在场内的小朋友的序号为 : " + ii);
}
}
}
/**
* 统计剩余的同学
*/
public int record(int[] re) {
int reco = 0;
for (int i : re) {
if (i0) {
reco = reco + 1;
} else {
continue;
}
}
// System.out.println("目前还剩下约瑟夫JAVA代码:" + reco + " 个小朋友在圈圈里面");
System.out.println("程序努力加载计算中.............................................................................................." + reco);
return reco;
}
public static void run() {
int childs[] = new int[500];
Yuesefu yuesefu = new Yuesefu();
yuesefu.markID(childs);
yuesefu.startGame(childs);
yuesefu.re_index(childs);
}
/**
* 游戏开始
*/
private void startGame(int[] i) {
int index = 0; // 用作循环
int leave = 0; // 用作离场
while (true) {
if (i[index] != 0) {// 还在场内
leave = leave + 1; // 报数
if (leave == 3) {// 判断是否需要离场 需要
i[index] = 0; // 离场
leave = 0;// 重新报数
}
index = index + 1;// 下一个人预备
if (index == i.length) {// 已经到最后了 需重新循环
index = 0;
}
} else {// 不在场内
if (index == i.length - 1) {// 判断此出场的人是否为最后一个人
index = 0;
}
index = index + 1;// 跳过
}
int x = new Yuesefu().record(i);// 统计剩余人数
if (x == 1)
break;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
run();
}
}
急!急!急!Java怎么用递归实现约瑟夫环?求试验成功的、初级的代码public class TestJosephus {
public static void main(String[] args) {

推荐阅读