题目:猴子选大王的小程序,有M只猴子彼此不服气都想争当猴王,打一架强者为王当然可行。但和平静气的解决才是王道,解决办法就是猴子们围成一圈,从1开始报数,每到规定的数N则将第N个猴子淘汰,然后继续从1开始报数继续按规则淘汰,胜者为王。最后剩下的猴子就是猴王,这样各凭运气,听天由命谁也不要抱怨。
使用简单数组的方式实现:
将编号存放在数组里,将下标作为报数时的移动,count变量为计数循环量,下标从0开始一直循环到数组的末尾时然后设置成数组的开始,使之变成一个循环数组,count变量从1开始,然后每到N时,将数组中对应的编号置0,然后count继续从1开始计数,当遇到编号为0的猴子时,表示该猴子已经淘汰,跳过该猴子下标继续移动。
确定使用的变量:
M=8 表示猴子的总数
N=3 表示淘汰规则的数字
猴子编号存储的数组monkeynum[]
0 1 2 3 4 5 6 7 下标
1 2 3 4 5 6 7 8 编号
king 存储猴王的编号
count 计数,名义上我们心中想的123然后123循环往复
程序代码:
//author:Monkey.D.Echo
//function:
//date:20170115 20:30
public class monkey{
public static void main (String args[]){
int M = 8;//猴子总数
int N = 3;//规定淘汰的数字编号
int monkeynum[] = new int[M];
for(int i=0;i<monkeynum.length;i++){
monkeynum[i]=i+1;
}
int count = 1;//循环计数
int king = 0;//猴子大王的编号
int index = 0;//数组的下标
while(M!=0){
//首先先把单数组变成一个循环数组,就是循环到数组的末尾时,把数组的下标制成数组的开始
if(index==monkeynum.length){
index=0;
}
//然后判断循环计数的情况
if(count==N && monkeynum[index]!=0){
monkeynum[index]=0;
M--;
count=1;
}
if(monkeynum[index]!=0){
count++;
}
index++;
for(int i=0;i<monkeynum.length;i++){
System.out.print(monkeynum[i]+" ");
}
System.out.println(" ");
}
king = index;
System.out.println("猴子的大王的编号是"+king);
}
}
条件设置有问题的情况下出现的错误:
//author:Monkey.D.Echo
//function:
//date:20170115 20:30
public class monkey{
public static void main (String args[]){
int M = 8;//猴子总数
int N = 3;//规定淘汰的数字编号
int monkeynum[] = new int[M];
for(int i=0;i<monkeynum.length;i++){
monkeynum[i]=i+1;
}
int count = 1;//循环计数
int king = 0;//猴子大王的编号
int index = 0;//数组的下标
while(M!=4){
//首先先把单数组变成一个循环数组,就是循环到数组的末尾时,把数组的下标制成数组的开始
if(index==monkeynum.length){
index=0;
}
//然后判断循环计数的情况;判断条件的设定很重要
//比如:在这里自己就设置错误
if(count==N){
monkeynum[index]=0;
M--;
count=1;
}
if(count!=N && monkeynum[index]!=0){
count++;
}
index++;
for(int i=0;i<monkeynum.length;i++){
System.out.print(monkeynum[i]+" ");
}
System.out.println(" ");
}
king = index;
System.out.println("猴子的大王的编号是"+king);
}
}
当循环条件设置成M!=4时出现错误的原因:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 0 4 5 6 7 8
1 2 0 4 5 6 7 8
1 2 0 4 5 6 7 8
1 2 0 4 5 0 7 8
1 2 0 4 5 0 7 8
1 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 5 0 7 8
0 2 0 4 0 0 7 8 count:3->count:1;index:5
0 2 0 4 0 0 7 8 count:1;index:6
0 2 0 4 0 0 7 8 count:2;index:7
0 2 0 4 0 0 7 8 count:3;index:1
0 2 0 4 0 0 7 8 count:3->count:1;monkeynum[index]=0;index:2;这里差了一个循环
相应的还有其他的方式:
//author:Monkey.D.Echo
//function:
//date:20170115 20:30
public class monkey{
public static void main (String args[]){
int M = 8;//猴子总数
int N = 3;//规定淘汰的数字编号
int monkeynum[] = new int[M];
for(int i=0;i<monkeynum.length;i++){
monkeynum[i]=i+1;
}
int count = 1;//循环计数
int king = 0;//猴子大王的编号
int index = 0;//数组的下标
while(M!=0){
if(index==monkeynum.length) index=0;
if(count==N&&monkeynum[index]!=0){
monkeynum[index]=0;
M--;
count++;
if(count==N+1) count=1;
}
if(monkeynum[index]!=0){
count++;
if(count==N+1) count=1;
}
index++;
for(int i=0;i<monkeynum.length;i++){
System.out.print(monkeynum[i]+" ");
}
System.out.println(" ");
}
king = index;
System.out.println("猴子的大王的编号是"+king);
}
}