12个人围成一圈.从1号开始报号,凡是数到5的人就走出圈子(出局).然后继续报号.试问最后一个出局的人是那一个
public static void out(int m,int n){
boolean[] people=new boolean[m];
int outCnt=0;
int index=0;
int tempCnt=1;
while(outCnt<m){
index=(index+1)%m;
if(!people[index]){
tempCnt=tempCnt+1;
if(tempCnt==n){
people[index]=true;
System.out.println("#"+(index+1)+" out");
outCnt++;
tempCnt=0;
}
}
}
}
本文介绍了一个简单的算法模拟问题,即12个人围成一圈进行报数游戏,每次数到5的人离开,直到最后一个人出局为止。通过使用布尔数组来标记已经出局的人,并迭代计算直至所有人都已出局。
4381

被折叠的 条评论
为什么被折叠?



