package com.kewin;
public class Demo4 {
public static void main(String[] args)
{
long starttime = System.currentTimeMillis();
Cyslink cyc= new Cyslink();
cyc.SetChild(9);
cyc.createlink();
cyc.show();
cyc.SetK(2);
cyc.SetM(2);
cyc.play();
long time2 = System.currentTimeMillis();
System.out.println("玩游戏共用了" + (time2 - starttime)/1000.0 + "秒");
}
}
class Child
{
int no;
Child nextchilae=null;
public Child(int a )
{
this.no=a;
}
}
class Cyslink
{
Child FirstChild=null;
Child temp =null;
int len=0;
int k =0;
int m =0;
public void SetChild(int len)
{
this.len=len;
}
public void SetK(int k)
{
this.k=k;
}
public void SetM(int m)
{
this.m=m;
}
public void createlink()
{
for (int i=1;i<=len;i++)
{
if (i==1) {
Child ch =new Child(i);
this.FirstChild=ch;
this.temp=ch;
}else
{
if (i==len)
{
Child ch =new Child(i);
temp.nextchilae=ch;
temp =ch;
temp.nextchilae=this.FirstChild;
}
else
{
Child ch =new Child(i);
temp.nextchilae=ch;
temp =ch;
}
}
}
}
public void play()
{
Child tmpchilde=this.FirstChild;
for (int i=1;i<this.k;i++)
{
tmpchilde=tmpchilde.nextchilae;
}
while (this.len !=1)
{
for (int i =1 ;i<this.m;i++)
{
tmpchilde=tmpchilde.nextchilae;
}
Child tmp22 =tmpchilde.nextchilae;
while (tmp22.nextchilae != tmpchilde)
{
tmp22=tmp22.nextchilae;
}
tmp22.nextchilae=tmpchilde.nextchilae;
System.out.println(tmpchilde.no+ "出局");
tmpchilde=tmpchilde.nextchilae;
this.len--;
}
System.out.println(tmpchilde.no);
}
public void show()
{
Child temChild=this.FirstChild;
do {
System.out.println(temChild.no+"####"+temChild.nextchilae.no);
temChild=temChild.nextchilae;
} while (temChild!=this.FirstChild);
}
}