Use java cycle singly linked list to solve the problem Joseph

What is the problem Joseph

It is said that the famous Jewish historian Josephus had the following story: After the Roman occupation Qiaotapate, 39 Jews and Josephus and his friends hid in a cave, 39 Jews decide would rather die and do not get caught enemies and decided a suicide, 41 individuals arranged in a circle, the first personal Countin, every third number reported to the people who have to commit suicide, then a re-count by the newspaper, until all suicide killed so far. However, Josephus and his friends did not want to comply. Start with a person, beyond the individual k-2 (for the first man has been crossed), and kill the k individuals. Next, k-1 and then over the individual, and the k-th individual kill. The process continues around the circle, until finally only the next person to leave, this person can continue to live. The problem is that, given and, beginning to stand somewhere in order to avoid execution?

Joseph's story comparing cruel, Joseph problems we changed a little game, rules of the game are as follows:

  Suppose numbered 1,2,3 ... n individual sitting in a circle hand in hand, the agreed number k (1 <= k <= n) of the number of packets began, the number of people out of m turns, his 1 and from a lower numbered off, and the number of people out of the m ring, and so on, until the remaining one is the winner, thereby generating a sequence number of a circle,

Circular list using n individuals sitting around analog

First, we define a circular linked list, thereby simulate n individuals sitting in a circle. Look under the following single chain implementation code

1  / * 
2  * unidirectional circular list
 . 3   * / 
. 4  class CycleSingleLinkedList {
 . 5      PersonalNode First = null ;
 . 6      / ** 
. 7       * num add nodes to the circular list
 . 8       * @param num
 . 9       * / 
10      public  void the Add ( int NUM) {
 . 11          IF (NUM <. 1 ) {
 12 is              System.out.printf ( "invalid parameters" );
 13 is              return ;
 14          }
 15          PersonalNode Curr = null;
16         for (int i = 1; i <=num; i++) {
17             PersonalNode personalNode = new PersonalNode(i);
18             if(i == 1){
19                 //添加第一个节点
20                 first = personalNode;
21                 curr = first;
22                 curr.setNext(first);
23             }else {
24                 curr.setNext(personalNode);
25                 personalNode.setNext(first);
26                 curr =PersonalNode;
 27              }
 28          }
 29  
30      }
 31  
32  }
 33  
34  / ** 
35  * person节点
 36   * / 
37  class PersonalNode {
 38      private  int no;
 39      private PersonalNode next;
 40  
41      public PersonalNode ( int no) {
 42          this . = no no;
 43      }
 44  
45      public  int GetNo () {
 46          return no;
47     }
48 
49     public void setNo(int no) {
50         this.no = no;
51     }
52 
53     public PersonalNode getNext() {
54         return next;
55     }
56 
57     public void setNext(PersonalNode next) {
58         this.next = next;
59     }
60 }

Below we explain the way the legend of the process to add n nodes circular list when creating the first node, we let the first and current points to the first node and the first node points to the current next, in order to form a loop. When adding the second node, the next will be a pointer to a second current node, next the second node to the first, and then move the current, i.e., current points to the second node, and so on.

 

Now that is the first step of our problems solved n individuals sitting together, to achieve the following number from the second step of the k began a few individuals. The second step is relatively simple, i.e. the first pointer is moved so that k-1 times, whereby the first pointer to make a first number of the number of people. However, due to first lap out of the node pointed to the need, where we then define a helper pointer to point to the first pointer before a node, that is, first of all let helper pointer points to first, and so the cycle until the next pointer is equal to the first helper (because it is a circular list ). And always keep the helper pointer in front of a first node pointer (only a first node and a pointer to the same node). I.e., k = 2 is assumed that the number of starts from the second individual, m = 3 i.e. the number of people out of the ring 3, as shown in FIG.

 

Find out who node4 circle, so that at this time is equal to the next helper Next first, and then let the next helper equal first, as shown in FIG.

 

Node1 continues from the start number, the number 3, i.e. node3 a circle, this time as a pointer to the case of FIG.

After node3 the ring to the next ring is node1, and so the cycle until the pointer to the first helper and the same node, i.e. node2 is the last one to the circle. code show as below

 1 /**
 2      * 约瑟夫问题
 3      * @param n n个人围成圈
 4      * @param k 第k个人开始报数
 5      * @param m 数到m的人出圈
 6      */
 7     public void joseph(int n,int k,int m){
 8         //添加n个人
 9         add(n);
10         //helper指针指向first的前一个节点
11         PersonalNode helper = first;
12         while (helper.getNext() != first){
13             helper = helper.getNext();
14         }
15         //找到第k个人
16         for (int i = 0; i < (k-1); i++) {
17             first = first.getNext();
18             helper = helper.getNext();
19         }
20         //数到m的人出圈
21         while (true){
22             if(helper == first){
23                 break;
24             }
25             for (int i = 0; i < (m-1); i++) {
26                 first = first.getNext();
27                 helper = helper.getNext();
28             }
29             System.out.printf("编号为%d的人出圈\n",first.getNo());
30             first = first.getNext();
31             helper.setNext(first);
32         }
33         System.out.println("最后出圈的人是:"+first.getNo());
34 
35     }

测试代码

1 public static void main(String []args){
2         CycleSingleLinkedList linkedList = new CycleSingleLinkedList();
3         linkedList.joseph(4,2,3);
4 }

测试结果:

 

 

总结

以上就是实现约瑟夫问题的循环链表解决思路,当然肯定还有很多别的解决思路,欢迎留言探讨,谢谢。

Guess you like

Origin www.cnblogs.com/menglong1108/p/11617542.html