Josephus problem: There are n personal cordons Arranging the order. From the first person countin (report number from 1-3), who report 3 people exit the circle, and asked who the last remaining original No. few.

First of all, my greatest source of learning but Baidu is not my group of friends here ~ ~ Table Bai Yibo group of friends and I love to learn!
Today, the group then suddenly someone raised this issue topic:
there are n personal cordons Arranging the order. From the first person countin (report number from 1-3), who report 3 people exit the circle, and asked who the last remaining original No. few .

Wrestling for a long time (OK, I admit that I would review the trial title on the Baidu ..), then found several algorithms it, but I do not know because I will only java or really some people thinking more widely jumping, so it looks a little complicated ah.

Then I have carefully looked a long time, barely understand a little better, but I think it is not simple, but is cruising on thinking.

But I also feel I've seen quite wide (mainly a lot of content) links in this sticky little, interested students can go and see: https://blog.csdn.net/weixin_38214171/article/details / 80352921

Then I went on the Internet to find a look, and sure enough I think to find a very straightforward and very convenient solution, then I made an explanation on its basis, the following is the solution:

 1 package test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /**
 7  *     约瑟夫环问题的解决
 8  * @author sijia
 9  *
10  */
11 public class Demo1 {
12 
13     public static void main(String[] args) {
14         List<Integer> list = new ArrayList<Integer>();
15         list.add(0);
16         list.add(1);
17         list.add(2);
18         list.add(3);
19         list.add(4);
20         list.add(5);
21         list.add(6);
22         list.add(7);
23         list.add(8);
24         list.add(9);
25         list.add(10);
26         list.add(11);
27         list.add(12);
28         list.add(13);
29         list.add(14);
30         List.add (15 );
 31 is          List.add (16 );
 32          
33 is          
34 is          // idea is to iterate, a delete two separated elements, until only one element. If a jump to the beginning end of the array is 
35          int I = 0 ;
 36          the while (! List.size () =. 1 ) {              
 37 [                  // traverse to the last element, i + 2 is a positive number to jump to the second element 
38                  IF (I> = list.size () -. 1 ) {
 39                      I = -1 ;
 40                  }
 41 is                  // traverse to the penultimate element, i + 2 is a jump to the first element 
42 is                  IF (I> = list.size () - 2 ) {
 43 is                      I = -2 ;
44                  }
 45                  // completes the jump to the beginning end of the array data array 
46 is                  
47                  I = I + 2 ;
 48                  / ** 
49                     * delete elements
 50                     * may be understood here not brought. First delete 0 + 2, which is the index of the element 2 is deleted. Second continue, come here
 51                     * i = 2 + 2 becomes 4. Why this marked as deleted under 4? Because the index is deleted before the 2, followed in turn forward the elements.
52                     * original index subscript 3 is now 2. Similarly, the original index is now at index 5 4.
 53 is                     * thus: 3,4, 5 behind the original standard element 4 holds many lessons under standard deleted. In line with the logic of the subject.
54                     * i + 2 each time, because the last round to delete an element, the overall index were advancing. So on behalf of every two will be +2 delete a
 55                     * Also we can think about this question of transformation: for example, the title becomes a delete every five, which is the number off 1-6 count to 6 out how to write?
56                  * 
 57                   * / 
58                  list.remove (i);
 59              
60          }
 61          / ** 
62           * final print out is the rest of that element, because I tricky here, the value of the index and the corresponding elements, so we not only As can be seen the value of the element
 63           * See also elements of the original index. If you can not do this on a copy of the list in the beginning, and then use the last remaining value to the thrust reverser, and then bad can not be removed but marked out by other means. To change such a relatively large
 64           * The total number of elements in the array with the values list, which is closely related to the subject of the N, anyway, I tried many times.
65           * / 
66          of System.out.print (List.get (0 ));
 67  
68      }
 69  
70 }

So far, the problem is solved, then the text left me two questions:

There is a certain relationship between the number n and 1. This number excluded m, but my math residue tests or a little impatient thinking over and over again, so as a legacy of it.

2. I said in the text of this approach is to implement a personal number N to exit 3 of the code. But if the topic is a personal number N 5 to quit? How should I write? Anyway, I currently have the ideas, interested private letter I can explore the oh ~ ~

Full hand to play is not easy, if a little help to you, please likes to point followers to support what ~~~~~ I wish you all work smoothly!

 

Guess you like

Origin www.cnblogs.com/lisijia/p/11454561.html