import java.util.ArrayList; \nimport java.util.List; \nimport java.util.Scanner; \n\npublic class JosephusCircle { \n public static void main(String[] args) { \n Scanner scanner = new Scanner(System.in); \n System.out.println("请输入总人数n:"); \n int n = scanner.nextInt(); \n System.out.println("请输入报数m:"); \n int m = scanner.nextInt(); \n System.out.println("请输入最终剩余人数p:"); \n int p = scanner.nextInt(); \n\n if (n < 2 || m < 2 || p >= n) { \n System.out.println("输入不符合要求"); \n return; \n } \n\n List circle = new ArrayList<>(); \n for (int i = 1; i <= n; i++) { \n circle.add(i); \n } \n\n int index = 0; \n while (circle.size() > p) { \n index = (index + m - 1) % circle.size(); \n circle.remove(index); \n } \n\n System.out.println("最终剩余的" + p + "个初始编号为:"); \n for (int i = 0; i < p; i++) { \n System.out.print(circle.get(i) + " "); \n } \n } \n}


原文地址: https://www.cveoy.top/t/topic/pqdH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录