Description
N个人围坐一圈,从1号开始连续报数,报到3的人退出圈,依此类推。直到最后圈里剩下两个人停止。问最后剩下的两个人的序号是多少。(报数按顺时针方向)
Input
圈中人数N
Output
剩下的两个人的序号
Sample Input
10
Sample Output
4 10
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
LinkedList l = new LinkedList();
for (int i = 1; i <= n; ++i) {
l.add(i);
}
int i = 0, k = 0;
while (l.size() > 2) {
if (i >= 2) {
i = 0;
l.remove(k);
} else {
++k;
if (k >= l.size()) {
k = 0;
}
++i;
}
}
System.out.print(l.get(0));
if (l.size() > 1) {
System.out.print(" " + l.get(1));
}
}
}