约瑟夫环

This commit is contained in:
zeek 2020-02-28 18:58:13 +08:00
parent 2ae04d828f
commit d08b39e9c5
2 changed files with 45 additions and 0 deletions

1
java/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.class

View 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);
}
}