import java.util.ArrayList; import java.util.List; import java.util.Scanner; //有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。 public class Test37 { public static void main(String[] args) { int n = getN(); List<Integer> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { list.add(i); } int count = 1; while (list.size() > 1) { for (int i = 0; i < list.size(); i++) { count++; if (count == 3) { list.remove(i); i--; count = 0; } } } System.out.println("留下的是原来第" + (list.get(0) + 1) + "号"); } public static int getN() { int n = 0; Scanner sc = new Scanner(System.in); System.out.println("请输入一个数:"); while (true) { String s = sc.nextLine(); try { n = Integer.parseInt(s); break; } catch (NumberFormatException e) { System.out.println("输入错误,请重新输入:"); } } return n; } }