//main
public class Test {
public static void main(String[] args) {
System.out.println("please input a num:");
int a = NumUtil.getInt();
List go = new ArrayList();
int b;
do {
System.out.println("please input a interval num:");
b = NumUtil.getInt();
if (b < a && b != 0) {
break;
} else {
System.out.println("must be littler than last one!");
}
} while (true);
int[] old=new int[a];
for(int i=0;i<a;i++){
old[i]=i+1;
}
int c=a;
int k=1;
do {
int num=0;
int length=old.length;
List teml=new ArrayList();
for(int j=0;j<length;j++){
if (k % b == 0) {
int temp=old[j];
teml.add(temp);
go.add(temp);
num++;
}
k++;
};
System.out.println(Arrays.toString(old));
old=NumUtil.remove(old,teml);
c-=num;
if(c==1){
break;
}
} while (true);
System.out.println(go.toString());
}
}
工具类
public class NumUtil {
public static int getInt(){
Scanner scan=new Scanner(System.in);
int a;
try {
a = scan.nextInt();
}catch(Exception e){
System.out.println("格式不正确!");
a=getInt();
scan.nextLine();
}
return a;
}
public static int[] remove(int[] list, List temp){
int a=0;
int[] na=new int[list.length-temp.size()];
for(int i=0;i<list.length;i++){
boolean flag=true;
for(int k=0;k<temp.size();k++){
if(list[i]==(int)temp.get(k)){
flag=false;
break;
}
}
if(flag){
na[a]=list[i];
a++;
}
}
return na;
}
}
//封装对象--------------------------------------------------------------------===================================-------------------------------------------------------------
public class ChildCyle {
private Child firstChild;
private Child temp;
private int childNum;
private int interval;
public void setChildLink(){
for(int i=1;i<=childNum;i++){
if(i==1){
Child ch=new Child(1);
this.firstChild=ch;
this.temp=ch;
}else if(i==childNum){
Child ch=new Child(i);
temp.setNextChild(ch);
ch.setNextChild(this.firstChild);
}else{
Child ch=new Child(i);
temp.setNextChild(ch);
this.temp=ch;
}
}
}
public void play(){
List go=new ArrayList();
temp=this.firstChild;
do{
for(int i=1;i<this.interval-1;i++){
temp=temp.getNextChild();
}
go.add(temp.getNextChild().getNo());
temp.setNextChild(temp.getNextChild().getNextChild());
temp=temp.getNextChild();
}while(temp!=temp.getNextChild());
System.out.println(go.toString());
}
public void show(){
temp=this.firstChild;
do{
System.out.println(temp.getNo());
temp=temp.getNextChild();
}while(temp!=firstChild);
}
public void setFirstChild(Child firstChild) {
this.firstChild = firstChild;
}
public void setTemp(Child temp) {
this.temp = temp;
}
public void setChildNum(int childNum) {
this.childNum = childNum;
}
public void setInterval(int interval) {
this.interval = interval;
}
}
class Child{
private int no;
private Child nextChild;
public Child(int no){
this.no=no;
}
public void setNo(int no) {
this.no = no;
}
public int getNo(){
return this.no;
}
public Child getNextChild(){
return this.nextChild;
}
public void setNextChild(Child nextChild) {
this.nextChild = nextChild;
}
}
class Test{
public static void main(String[] args) {
ChildCyle cc=new ChildCyle();
cc.setChildNum(7);
cc.setInterval(2);
cc.setChildLink();
// cc.show();
cc.play();
}
}