贴出我的代码,验证过几组数据,都是ok的
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入人数:");
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
al.add(i);
}
int i = 0;
while(al.size() > 1) {
i += 2;
while(i > al.size() - 1) {
i = i - al.size();
}
al.remove(i);
}
System.out.println("最后一个人的编号是:"+al.get(0));
}