Java古罗马有一个残酷的杀死犯人的游戏

来源:百度知道 编辑:UC知道 时间:2024/06/01 23:32:15
古罗马有一个残酷的杀死犯人的游戏。若干个犯人站成一圈,现任意处死一个犯人,然后从被杀死的犯人开始,以某个固定的步长数数,被数到的犯人就被处死。这个过程一直持续下去,一直到只剩下一个犯人为止。
但为了使题目更精彩,我们增加了一个新的规定:为了纪念被处死的犯人,每处死一个犯人后,他/她的号就会被增加到步长上去。
给你犯人总数、第一个被处死的犯人号、以及初始步长,你能用Java程序预测哪个犯人最终幸运地活下来吗?
我的邮箱490230270@qq.com~~~谢谢~~~

程序已发送,楼主注意查收
发信人:lixiaoyu198588@163.com
文件名:LiveAlone.java

public class CountOff{
public static void main(String[] args){
whoIsLast(1,3,500);
}
public static void whoIsLast(int first,int step,int count){
boolean[] people=new boolean[count];
int index,i,j,killed=0;
for(i=0;i<count;i++){
people[i]=true;
}
System.out.print(first+"\t");
step+=first;
people[first-1]=false;
for(i=1,index=first-1;i<count;i++){
for(j=0;j<step;){
if(people[index]){
j++;
}
killed=index++;
index%=count;
}
people[killed]=false;
step+=killed+1;
}
System.out.print((killed+1)+"\t");
}
}