Skip to content

Instantly share code, notes, and snippets.

@zhjwpku
Created September 14, 2017 12:32
Show Gist options
  • Save zhjwpku/1cb490680faaf1bbe831d247c5e4f1c7 to your computer and use it in GitHub Desktop.
Save zhjwpku/1cb490680faaf1bbe831d247c5e4f1c7 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Josephus {
public static List<Integer> getResult(int n, int m) {
List<Integer> circle = new ArrayList<>();
for (int i = 1; i <= n; i++) {
circle.add(i);
}
int k = 0;
while (circle.size() >= m) {
k = k + m;
k = k % (circle.size()) - 1;
if (k < 0) {
circle.remove(circle.size()-1);
k = 0;
} else {
circle.remove(k);
}
}
return circle;
}
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入总人数:");
int n = scanner.nextInt();
System.out.print("请输入报数的大小:");
int m = scanner.nextInt();
List<Integer> res = Josephus.getResult(n, m);
for (Integer i : res) {
System.out.println(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment