Prove safety offer46: circle the last remaining figures (a list, recursively)

1 Title Description

  Children's Day each year, cow-off will prepare some small gifts to visit the orphanage children, this year is also true. As an experienced veteran off HF cattle, naturally we prepared some games. Among them, there is a game like this: First, let the children surrounded by a large circle. He then randomly assigned a number m, so that number of children 0 gettin number. Cried every time m-1 the children sing a song to be out of the line, then any of the gift box in choosing a gift, and not return to the circle, starting with his next child, continue 0 ... m -1 number of packets .... go on .... until the last remaining child, you can not perform, and get the cattle off the rare "Detective Conan" Collector's Edition (limited places oh !! ^ _ ^). You want to try next, which the children will get the gifts it? (NOTE: The number of children is from 0 to n-1) if no children, return -1.

2 ideas and methods

  A thought :

  Consider the endless chain solution; stl used to simulate the list circular linked list. When the list itself is not an annular structure, so whenever the iterator to the end of the scan chain, the head needs to move the iterator to the list, the list of deleted nodes erase method, the iterator will fail the original, it is necessary to hold the next node. Vector using analog or circular linked list.

  Thinking two:

  https://blog.csdn.net/SCS199411/article/details/92988332

  Every rule is deleted digital analysis calculated directly circle last remaining digits. Implicit function f (n, m) of the n digits each time the m-th digit remove the last remaining figures.

Knowledge Point:

  Some properties of the modulo operation need to know: a, I take a number of times equal to the time taken, for example, 5% 2 = 1, and 5% 2% 2% 2 = 1; two, modulo operation is associative, e.g. a% n - 1 = (a-1)% n (n> 1); three, if not [0, n-1] interval the number of x, there is the number of congruence of the interval for a clear and only one. For example, x = -2, -2% n = n-2.

3 C ++ core code

. 1  class Solution {
 2  public :
 . 3      int LastRemaining_Solution ( int n-, int m)
 . 4      {
 . 5          // circular linked list
 . 6          // the STL is not only through the bad list, the list head to be redirected at each visit to the end of the list 
. 7          int LastRemaining_Solution ( int n-, int m) {
 . 8              IF (n-< . 1 || m < . 1 )
 . 9                  return  0 ;
 10  
. 11              List < int > the nums;
 12 is              for (int i = 0; i < n; ++i) {
13                 nums.push_back(i);
14             }
15 
16             list<int>::iterator cur = nums.begin();
17             while (nums.size()>1){
18                 for (int i = 1; i < m; ++i) {
19                     cur++;
20                     if(cur == nums.end())
21                         cur = nums.begin();
22                 }
23 is                  List < int > :: = Iterator Next CUR ++;    // at a storage node 
24                  IF (== Next nums.end ())
 25                      Next = nums.begin ();
 26 is                  cur-- ;
 27                  nums.erase (CUR);     // delete the node 
28                  CUR = next;          // to point to the next node 
29              }
 30              return * (CUR);
 31 is      }
 32 };
View Code
 1 class Solution {
 2 public:
 3     int LastRemaining_Solution(int n, int m)
 4     {
 5         if(n < 1 || m < 1)        //守卫代码
 6             return -1;
 7 
 8         //int last = 0;
 9         //for (int i = 2; i<=n; ++i){
10         //   last = (last+ m) % i;
11         //}
12         //return last;
13 
14         if(n == 1)
15             return 0;
16         return (LastRemaining_Solution(n-1, m) + m) % n;
17     }
18 };    
View Code

4 C ++ complete code

 1 #include <iostream>
 2 #include <vector>
 3 #include <list>
 4 
 5 using namespace std;
 6 
 7 int LastRemaining_Solution(int n, int m);
 8 
 9 int main() {
10     cout << LastRemaining_Solution(5, 3) << endl;
11 
12     system("pause");
13     return 0;
14 }
15 
16 intLastRemaining_Solution ( int n-, int m) {
 . 17      IF (n-< . 1 || m < . 1 ) return - . 1 ;
 18 is  
. 19      // used to simulate a circular linked list, vector 
20 is      vector < int > NUM;
 21 is      for ( int I = 0 ;! = n-I; I ++ ) {
 22 is          num.push_back (I);
 23 is      }
 24  
25      int Start = 0 ;
 26 is      the while (num.size ()> . 1 ) {
27          int cntDown = m;
 28          int Music Videos = Start;
 29          the while (- cntDown) {
 30              // If traverse to the last digit, the next first number through the array 
31 is              IF (Music Videos == (num.size () - . 1 )) {
 32                  Music Videos = 0 ;
 33 is              }
 34 is              the else {
 35                  Music Videos ++ ;
 36              }
 37 [          }
 38 is  
39          IF (Music Videos == (num.size () - . 1 )) {
 40              Start =0 ;
41          }
 42          else {
 43              start = the like;
44          }
 45          num.erase (num.begin () + , etc.);
46      }
 47  
48      Return num [ 0 ];
49 }
View Code

Reference material

https://blog.csdn.net/zjwreal/article/details/88917056

https://blog.csdn.net/m0_37950361/article/details/82154753

https://blog.csdn.net/SCS199411/article/details/92988332

 

Guess you like

Origin www.cnblogs.com/wxwhnu/p/11423404.html