题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
package cn.xxx.interview.singleLooplist;
public class test {
// SingleLoopList list = new SingleLoopList();
static int leftcount;
static int count;
static int index;
static Boolean[] b;
public static void main(String[] args) {
handleLoop(10);
}
private static void handleLoop(int n) {
b = new Boolean[n];
index = 0;
leftcount = n;
count = 0;
for (int i = 0; i < n; i++) {
b[i] = true;
}
while (leftcount > 1) {
if (b[index] == true) {
count++;
if (count == 3) {
count = 0;
b[index] = false;
leftcount--;
}
}
index++;
if (index == n) {
index = 0;
}
}
for (int i = 0; i < n; i++) {
if (b[i] == true) {
System.out.println(i);
}
}
}
}