package com.cy.ideatest;
import java.util.*;
/**
* 有n个人围成一圈,
* 顺序排号。从第一个人开始报数(从1到3报数),
* 凡报到3的人退出圈子,问最后留下的是原来第几号的那位
*/
public class QuanQuanBaoShu {
public static void main(String[] args) {
System.out.println("请输入围圈总人数:");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
List<Integer> mans = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
mans.add(i + 1);
}
System.out.println("初始化编号为" + mans + "的" + mans.size() + "个人围成一圈");
ListIterator<Integer> iterator = null;
int k = 1;
do {
iterator = mans.listIterator();
while (iterator.hasNext()) {
Integer next = iterator.next();
if (k++ % 3 == 0) {
System.out.println("编号" + next + "的人退出圈子");
iterator.remove();
k = 1;
}
}
} while (mans.size() > 1);
System.out.println("剩下编号为" + mans + "的" + mans.size() + "个人");
}
}
04-05
1584
10-22
1439
02-20
371
02-12
2716