package other;
/**
* 类似于约瑟夫环游戏
* 给定步长报数,不考虑传入数组长度小于步长
* */
public class RingTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ring = {1,2,3,4,5,6,7,8,9,10,11,12};
int step = 3;
ringQuit1(ring,step);
}
/**
* 最基本的解法
* */
public static void ringQuit1(int[] ringArray,int step){
int tempStep=0;
int leftLength=ringArray.length;
while(leftLength>=step){
System.out.print("报数:");
for(int i=0;i<ringArray.length;i++){
if(ringArray[i]>0){
tempStep++;
if(tempStep==step){
System.out.print(ringArray[i]+" ");
tempStep=0;
ringArray[i]=-1;
leftLength--;
}
}
}
tempStep=0;
System.out.println("\n本轮报数剩余成员如下");
printLeft(ringArray);
}
System.out.println("报数完成");
}
/**
* 打印剩余成员
* */
public static void printLeft(int[] array){
for(int i:array){
if(i>0){
System.out.print(i+" ");
}
}
System.out.println();
}
}
转载于:https://my.oschina.net/sharkbobo/blog/270205
本文介绍了一种类似于约瑟夫环游戏的算法实现,通过给定的步长进行报数,每次达到步长的人将被淘汰,直到最后剩下的人。文章提供了一个基本的Java实现示例,展示了如何在数组中进行报数并移除达到步长的人。
2814

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



