Browse Source

上传文件至 ''

2207190425 1 year ago
parent
commit
b73b423163
2 changed files with 36 additions and 0 deletions
  1. 35 0
      JosephRing.java
  2. 1 0
      Read.md

+ 35 - 0
JosephRing.java

@@ -0,0 +1,35 @@
+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;
+            }
+        }
+    }
+}

+ 1 - 0
Read.md

@@ -0,0 +1 @@
+疯狂星期四