package xunhuan;
// TODO 自动生成的方法存根
class Child{
int no;
Child next;
public Child(int nol){
no=nol;
next=null;
}
}
package xunhuan;
class Joseph{
int n,m;
Child first;
public Joseph(int n1,int m1) {
Child p,t;
n=n1;m=m1;
first=new Child(1);
t=first;
for(int i=2;i<=n;i++) {
p=new Child(i);
t.next=p;t=p;
}
t.next=first;
}
public String Jsequence() {
String ans="";
int i,j;
Child p,q;
for(i=1;i<=n;i++) {
p=first;
j=1;
while(j<m-1) {
j++;
p=p.next;
}
q=p.next;
ans+=q.no+"";
p.next=q.next;
first=p.next;
}
return ans;
}
}
package xunhuan;
import java.io.FileNotFoundException;
public class Exam {
public static void main(String[] args) throws FileNotFoundException{
System.out.println("************测试1*************");
int n=6,m=3;
Joseph L=new Joseph(n,m);
System.out.println("n="+n+",m="+m+"的Joseph序列");
System.out.println("************测试2*************");
n=8;m=4;
L=new Joseph(n,m);
System.out.println("n="+n+",m="+m+"的Joseph序列");
System.out.println(L.Jsequence());
}
}