/**
* 回溯算法
*
* 约瑟芬杀人问题
* */
public class Josephus {
private static int N = 20;
private static int M = 5; //数到M就咔嚓一个人
class Node{
int val; //下标
Node next;
public Node(int val){
this.val = val;
}
}
public void killNode(){
Node header = new Node(1); //第一个结点
Node x = header; //目前被点到的人
for(int i = 2;i <= N;i++){
x = (x.next = new Node(i));
}
x.next = header; //头尾相接
System.out.println("被咔擦的顺序为:");
while(x != x.next){
//至少还有两人 仍然继续报数,咔擦
for(int i = 1;i < M;i++){
x = x.next;
}
System.out.println(x.next.val + "被咔擦了");
x.next = x.next.next;
}
System.out.println("最后的幸运儿是:" + x.val);
}
public static void main(String[] args){
Josephus j = new Josephus();
j.killNode();
}
}