约瑟夫环(Josephus)问题:古代某法官要判决n个犯人的死刑,他有一条荒唐的法律,将犯人站成一个圆圈,从第s个人开始数起,每数到第d个犯人,就拉出来处决,然后再数d个,数到的人再处决……直到剩下的最后一个可赦免.
public class Josephus {
private static class Node {
public int no;// 编号
public Node next;// 下一个节点
public Node(int no) {
this.no = no;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入总人数:");
int totalNum = scanner.nextInt();
System.out.print("请输入报数的大小:");
int cycleNum = scanner.nextInt();
Node header = new Node(1);
Node pointer = header;
System.out.println("header==pointer?"+(header==pointer));
System.out.println("header.equals(pointer)?"+(header.equals(pointer)));
for (int i = 2; i <= totalNum; i++) {
pointer.next = new Node(i);
pointer = pointer.next;
}
pointer.next = header;
// 初始化环形链表结束
System.out.println("以下是出列的顺序:");
while (pointer != pointer.next) {
for (int i = 1; i < cycleNum; i++) {
pointer = pointer.next;
}
System.out.println(pointer.next.no);
pointer.next = pointer.next.next;
}
System.out.println(pointer.next.no);
}
}