class OutOfCircle { public OutOfCircle(int nn, int mm) { n = nn; m = mm; man =newint[n]; //使用man數組表示N個人,man[i]為1表示i還在圈中,為0則表示i已經不在圈中 count =newint[n]; //保存出圈順序 java.util.Arrays.fill(man, 1); //初始化man,一開始所有人都在圈中,所以都為1 } publicint[] out() { int c =0; //當前人報的數 int j =0; while (total(man) !=0) { //當圈中沒人時,man中元素之和為0 for (int i =0; i < n; i++) { c = c + man[i]; //報數,出去的人為0,相當于沒報 if (c !=0&& c % m ==0) { //表示當前c!=0一定要加上,因為0對任何數取余都為0 man[i] =0; //出圈,置為0 count[j++] = i +1; //保存出圈人的編號 c =0; //重新開始報數 } } } return count; } privateint total(int[] t) { //求INT數組的和 //int sum =0; for (int i : t) { //sum += i;
if(t[i]!=0) return 1; } return 0; //return sum; } privateint n; privateint m; privateint[] man; privateint[] count; }