解法:
public static Queue<Integer> Josephus(int num, int integer){
Queue<Integer> survivors = new Queue<>();
Queue<Integer> deads = new Queue<>();
for(int i = 0; i < num; i++){
survivors.enqueue(i);
}
for(int i = 1; survivors.size() != 0; i++){
int person = survivors.dequeue();
if((i%integer) == 0){
deads.enqueue(person);
}else{
survivors.enqueue(person);
}
}
return deads;
}
测试:
public static void main(String[] args) {
System.out.println(Josephus(7, 2));
}
结果:
1 3 5 0 4 2 6