在围圈游戏中,规则是从某个数同时开始逆时针和顺时针走一定步长,淘汰对应索引的人员,则有两种情况可能顺逆时针轮到同一个人或许人不同,可以通过一个函数确定每走一步数数的索引值。
用一个数组表示每个人是否out 的标志位,初始数组每个值为1. out后为0
/**
* @param q 逆时针起始坐标
* @param step 方向
* @param p 步长
* @return 剔除的一到两个数的编号
*/
public static int chooseOne(int q,int step,int p,int[] a){
//用一个for 循环表示移动过程
for(int j=1;j<=p;j++ ){
//每次走一步的索引下标q; q=1~n;
do{
q=(q+step+n-1)%n+1; //更新Q 坐标
}while(a[q]==0); //当a[j]=0 不断找到下一步坐标, a[j]!=0,跳出while 进入下一次for循环的步长
}
for (int i = 1; i < a.length; i++) {
System.out.print(": "+a[i]);
}
System.out.println("output Element: "+q);
a[q]=0;
return q;
}