1234567891011121314151617181920212223242526272829303132333435 |
- package org.yuesefu;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class JosephRing {
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入人数n和m的值(m为1~5之间的整数):");
- int n = scanner.nextInt();
- int m = scanner.nextInt();
- scanner.close();
- List<Integer> people = new ArrayList<>();
- for (int i = 1; i <= n; i++) {
- people.add(i);
- }
- josephRing(people, m);
- }
- public static void josephRing(List<Integer> people, int m) {
- int count = 0;
- int index = 0;
- while (!people.isEmpty()) {
- count++;
- index = (index + 1) % people.size();
- if (count == m) {
- System.out.print(people.get(index) + ", ");
- people.remove(index);
- count = 0;
- }
- }
- }
- }
|