05_3_小孩数数问题
1. 例子
package Test;
/**
* 小孩数数问题,500个小孩,数数,从1开始,数到三退出。求最后一个退出的人原来的位置
* @author huigui
*
*/
public class Count3Quit {
public static void main(String[] args) {
boolean[] a = new boolean[500];
for (int i = 0; i < a.length; i++) {
a[i] = true;
}
int leftCount = a.length;
int index = 0;
int num = 0;
while(leftCount > 1) {
if(a[index] == true) {
num++;
if(num == 3) {
a[index] = false;
num = 0;
leftCount--;
}
}
index++;
if (index == a.length) {
index = 0;
}
}
for (int i = 0; i < a.length; i++) {
if(a[i] == true) {
System.out.println(i);
}
}
}
}