约瑟夫环问题(Joseph)

“约瑟夫生者死者”游戏内容大致描述为:30个游客同乘一条船,因为严重超载,加上风浪大作,危险万分。因此,船长告诉乘客,只有将船上一半的旅客投入大海中,其余的人才能幸免于难。无奈,大家只得同意这种办法,并议定30个人围城一圈,由第一个人数起,依次报数,数到第9人,便把他投入大海中,然后再从他的下一个数起,数到第9人,再把他扔进大海中,如此重复地进行,直到剩下15个乘客为止。请编程模拟此过程。

不带头结点,带头指针和尾指针的单线循环链表实现(参数可调控):

  1.  
    package data_structure_experiment2_linkedlist.applied_design_experiment;
  2.  
     
  3.  
    import java.util.Scanner;
  4.  
     
  5.  
    public class JosephCycle<T> {
  6.  
    public static void main(String[] args) {
  7.  
    Scanner scanner = new Scanner(System.in);
  8.  
    System.out.print("Enter the number of nodes in Joseph's cycle: ");
  9.  
    int numberOfJosephNode = scanner.nextInt(); //创建的约瑟夫环所含的结点个数
  10.  
    JosephCycle<Integer> josephCycle = new JosephCycle();
  11.  
    for (int i = 0; i < numberOfJosephNode; i++) {
  12.  
    josephCycle.add(new JosephCycleNode(i + 1));
  13.  
    }
  14.  
    System.out.println("The Joseph cycle was successfully created: ");
  15.  
    josephCycle.print();
  16.  
    System.out.print("Enter the gap of deaths to die: ");
  17.  
    final int gapOfDeaths = scanner.nextInt(); //投入大海间隔的人数(每次数到gapOfDeaths即投入大海)
  18.  
    System.out.print("Enter the number of deaths to die: ");
  19.  
    final int numberOfDeaths = scanner.nextInt(); //投入大海的人数
  20.  
    josephCycle.josephCycleLifeOrDeath(gapOfDeaths, numberOfDeaths);
  21.  
    System.out.println("After the man was killed, joseph circled the people left behind: ");
  22.  
    josephCycle.print();
  23.  
    /*int curNumberOfDeaths = 0; //以下注释了的代码,为封装成方法前编写的代码,可以忽略。
  24.  
    int indexOfJosephCycle = 0;
  25.  
    int indexOfCount = 0;
  26.  
    JosephCycleNode<Integer> josephCycleNode = josephCycle.getHead();
  27.  
    while (curNumberOfDeaths<numberOfDeaths){
  28.  
    josephCycleNode = josephCycleNode.getNext();
  29.  
    indexOfJosephCycle = (indexOfJosephCycle+1)%josephCycle.getNumberOfNode();
  30.  
    indexOfCount = (indexOfCount+1)%gapOfDeaths;
  31.  
    if (indexOfCount==gapOfDeaths-1){
  32.  
    josephCycle.delete(indexOfJosephCycle);
  33.  
    indexOfJosephCycle--;
  34.  
    curNumberOfDeaths++;
  35.  
    }
  36.  
    }
  37.  
    josephCycleNode = josephCycle.getHead();
  38.  
    for (int i = 0; i < josephCycle.getNumberOfNode(); i++) {
  39.  
    System.out.print(josephCycleNode.getData()+"\t");
  40.  
    josephCycleNode = josephCycleNode.getNext();
  41.  
    }*/
  42.  
    }
  43.  
     
  44.  
    private JosephCycleNode<T> head;
  45.  
    private JosephCycleNode<T> tail;
  46.  
    private int numberOfNode = 0;
  47.  
     
  48.  
    public JosephCycle() {
  49.  
    }
  50.  
     
  51.  
    public JosephCycleNode<T> getHead() {
  52.  
    return head;
  53.  
    }
  54.  
     
  55.  
    public JosephCycleNode<T> getTail() {
  56.  
    return tail;
  57.  
    }
  58.  
     
  59.  
    public int getNumberOfNode() {
  60.  
    return numberOfNode;
  61.  
    }
  62.  
     
  63.  
    public void add(JosephCycleNode<T> node) {
  64.  
    if (head == null) {
  65.  
    head = tail = node;
  66.  
    node.setNext(node);
  67.  
    } else {
  68.  
    tail.setNext(node);
  69.  
    tail = node;
  70.  
    tail.setNext(head);
  71.  
    }
  72.  
    numberOfNode++;
  73.  
    }
  74.  
     
  75.  
    public void delete(int index) {
  76.  
    if (index < 0) {
  77.  
    throw new RuntimeException("The index of the deleted node cannot be negative.The deletion failed!");
  78.  
    }
  79.  
    if (index == 0) {
  80.  
    tail.setNext(head.getNext());
  81.  
    head = tail.getNext();
  82.  
    numberOfNode--;
  83.  
    return;
  84.  
    }
  85.  
    int indexOfJosephCycle = 0;
  86.  
    JosephCycleNode josephCycleIndexNode = head;
  87.  
    for (; josephCycleIndexNode.getNext() != head && indexOfJosephCycle < index - 1; indexOfJosephCycle++) {
  88.  
    josephCycleIndexNode = josephCycleIndexNode.getNext();
  89.  
    }
  90.  
    if (indexOfJosephCycle == index - 1) {
  91.  
    josephCycleIndexNode.setNext(josephCycleIndexNode.getNext().getNext());
  92.  
    if (index == numberOfNode - 1) {
  93.  
    tail = josephCycleIndexNode;
  94.  
    }
  95.  
    numberOfNode--;
  96.  
    } else {
  97.  
    throw new RuntimeException("The number of nodes in Joseph's cycle is:" + ++indexOfJosephCycle);
  98.  
    }
  99.  
    }
  100.  
     
  101.  
    public void josephCycleLifeOrDeath(int gapOfDeaths, int numberOfDeaths) {
  102.  
    if (numberOfDeaths >= numberOfNode) {
  103.  
    throw new RuntimeException("The death toll is greater than or equal to the total number of deaths");
  104.  
    }
  105.  
    int curNumberOfDeaths = 0;
  106.  
    int indexOfJosephCycle = 0;
  107.  
    int indexOfCount = 0;
  108.  
    JosephCycleNode<T> josephCycleNode = head;
  109.  
    while (curNumberOfDeaths < numberOfDeaths) {
  110.  
    josephCycleNode = josephCycleNode.getNext();
  111.  
    indexOfJosephCycle = (indexOfJosephCycle + 1) % numberOfNode;
  112.  
    indexOfCount = (indexOfCount + 1) % gapOfDeaths;
  113.  
    if (indexOfCount == gapOfDeaths - 1) {
  114.  
    delete(indexOfJosephCycle);
  115.  
    indexOfJosephCycle--;
  116.  
    curNumberOfDeaths++;
  117.  
    }
  118.  
    }
  119.  
    }
  120.  
     
  121.  
    public void print() {
  122.  
    JosephCycleNode<T> josephCycleNode = head;
  123.  
    for (int i = 0; i < numberOfNode; i++) {
  124.  
    System.out.print(josephCycleNode.getData() + "\t");
  125.  
    josephCycleNode = josephCycleNode.getNext();
  126.  
    }
  127.  
    System.out.println();
  128.  
    }
  129.  
     
  130.  
    }
  131.  
     
  132.  
    class JosephCycleNode<T> {
  133.  
    private T data;
  134.  
    private JosephCycleNode next;
  135.  
     
  136.  
    public JosephCycleNode() {
  137.  
    }
  138.  
     
  139.  
    public JosephCycleNode(T data) {
  140.  
    this.data = data;
  141.  
    }
  142.  
     
  143.  
    public T getData() {
  144.  
    return data;
  145.  
    }
  146.  
     
  147.  
    public void setData(T data) {
  148.  
    this.data = data;
  149.  
    }
  150.  
     
  151.  
    public JosephCycleNode getNext() {
  152.  
    return next;
  153.  
    }
  154.  
     
  155.  
    public void setNext(JosephCycleNode next) {
  156.  
    this.next = next;
  157.  
    }
  158.  
    }

 

posted @ 2020-10-21 12:20  5118会员优惠码  阅读(112)  评论(0编辑  收藏  举报

bug专家https://www.bugzj.com/

投资理财