约瑟夫环
This commit is contained in:
parent
2ae04d828f
commit
d08b39e9c5
1
java/.gitignore
vendored
Normal file
1
java/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.class
|
44
java/bishi/YueSeFuHuan.java
Normal file
44
java/bishi/YueSeFuHuan.java
Normal file
@ -0,0 +1,44 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
public class YueSeFuHuan {
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
while (in.hasNext()) {
|
||||
int m = in.nextInt();
|
||||
if (m <=1 || m>=100){
|
||||
System.out.println("ERROR!");
|
||||
continue;
|
||||
}
|
||||
Map<Integer, Integer> numList = new HashMap<>();
|
||||
for (int i=1;i<=100;i++){
|
||||
numList.put(i, i);
|
||||
}
|
||||
count(numList, m, 1, numList.size());
|
||||
}
|
||||
}
|
||||
|
||||
private static void count(Map<Integer, Integer> numList, int m, int step, int maxSize){
|
||||
if (maxSize < m){
|
||||
// 打印结果
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Integer i =1;i<= maxSize;i++){
|
||||
sb.append(numList.get(i)).append(",");
|
||||
}
|
||||
System.out.println(sb.toString().substring(0,sb.lastIndexOf(",")));
|
||||
return;
|
||||
}
|
||||
int s = 1;
|
||||
for (int i=1;i<= maxSize;i++){
|
||||
if (step == m){
|
||||
step = 1;
|
||||
continue;
|
||||
}
|
||||
numList.put(s, numList.get(i));
|
||||
step ++;
|
||||
s ++;
|
||||
}
|
||||
count(numList, m, step, s-1);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user