












作业:上机实习题目
1、Josephu问题(丢手帕问题)
Josephu问题为:设编号为1,2,...n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
提示:用一个不带头结点的循环链表来处理Josephu问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点的人从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。
//Josephu问题(丢手帕问题)
//使用单向链表
public class Demo121 {
public
static void main(String[] args) {
CycLink
cyclink=new CycLink();
cyclink.setLen(5);//链表长度
cyclink.createLink();
cyclink.setK(2);//从第几个人开始数
cyclink.setM(2);//数几下
cyclink.show();
cyclink.play();
}
}
class Child{
int
no;
Child
nextChild=null;
public
Child(int no){
//给一个编号
this.no=no;
}
}
//单向环形链表
class CycLink{
//先定义一个指向链表第一个小孩的引用
//指向第一个小孩的引用,不能动
Child
firstChild=null;
Child
temp=null;
int
len=0;//表示共有多少个小孩
int
k=0;
int
m=0;
//设置m数几下
public
void setM(int m){
this.m=m;
}
//设置环形链表大小
public
void setLen(int len){
this.len=len;
}
//设置从第几个人开始数数
public
void setK(int k){
this.k=k;
}
//开始play
public
void play(){
Child
temp=this.firstChild;
//1.先找到开始数数的人
for(int
i=1;i<k;i++){
temp=temp.nextChild;
}
while(this.len!=1){
//2.数m下
for(int
j=1;j<m;j++){
temp=temp.nextChild;
}
//找到要出圈的前一个小孩
Child
temp2=temp;
while(temp2.nextChild!=temp){
temp2=temp2.nextChild;
}
//3.将数到m的小孩,退出圈
temp2.nextChild=temp.nextChild;
//让temp指向下一个数数的小孩
temp=temp.nextChild;
this.len--;
}
//最后一个小孩
System.out.println("最后出圈的小孩:"+temp.no);
}
//初始化单向环形链表
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.nextChild=ch;
temp=ch;
temp.nextChild=this.firstChild;
}else{
//继续创建小孩
Child
ch=new Child(i);
temp.nextChild=ch;
temp=ch;
}
}
}
}
//打印该环形链表
public
void show(){
//定义一个跑龙套
Child
temp=this.firstChild;
do{
System.out.print(temp.no+"
");
temp=temp.nextChild;
}while(temp!=this.firstChild);
}
}
