在圍圈遊戲中,規則是從某個數同時開始逆時針和順時針走一定步長,淘汰對應索引的人員,則有兩種情況可能順逆時針輪到同一個人或許人不同,可以通過一個函數確定每走一步數數的索引值。
用一個數組表示每個人是否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;
}