C + + uses circular link list to solve Joseph ring problem

Source: Internet
Author: User

Joseph Ring Question

Known n individuals (n>=1) around a round table, starting from 1 sequential numbering, from the number of 1 people began to count, clockwise to M of the person out. The next man began counting off from 1, and the man who counted to M was out again. Repeat this rule until everyone is out of the way. May I ask the initial number of the last person to dequeue.

Requirements

Enter the number of people N, a count of M, output the last person's initial number.

Solution Ideas

    • First, because it is a round table problem, the use of a linked list to solve the need to build a circular link list.
    • Then there is the problem of the dequeue, where my idea is to move the pointer to the linked list to the position of the person who needs to be out of the way, and then follow the normal list to delete the operation.
    • Finally, the design of the class, can be the head, M, N, linked list construction, linked list deletion and so on together to encapsulate.

Code

1 //6.cpp--Using gcc compiler2#include <iostream>3 using namespacestd;4 5typedefstructList6 {7     intnum;8     structList *Next;9}*pList;Ten  One classJosephus A { -      Public: - Josephus () {} theJosephus (intNumberintmes): - N (number), - m (MES) {} -         void Set(); +         voidcreat (); -         voiddel (); +     Private: A pList head; at         intN, M, tmp_n; - }; -  - voidJosephus::Set() - { -cout <<"Please input the number of the people:"; inCIN >>N; -Tmp_n =N; tocout <<"Please input the M:"; +CIN >>m; - } the  * voidjosephus::creat () $ {Panax Notoginseng pList p1, p2; -PList p =NewList; then + =1; +num = P1; AP2 = head =p; the      for(inti =2; I < n; i++) +     { -p =NewList; $num = Pi; $  -P1 =P2; -P2 =p; theP1-Next =P2; -     }Wuyi  theP2-Next =head; -     //Output each number of the Circle list 's members. Wu     //and It should be:1, 2, ..., n -p =head; Aboutcout <<"Now , the ' num\ ' member of the list is:"<<Endl; $      while(n--) -     { -         if(n = =0) -cout << p-> Num <<"."; A         Else +         { thecout << P->num <<", "; -p = PNext; $         } the     } the } the  the voidJosephus::d El () - { inPList P1 =NULL; thepList P2 =head; the  Aboutn = tmp_n +1; the      while(n--) the     { the         ints = M-1; +          while(s--) -         { theP1 =P2;BayiP2 = P2Next; the         } the          -         if(n = =0) -         { theP2 = P2Next; theP1-Next =NULL; thecout <<"The result is:"<< P2 num <<Endl; the         } -         Else  the         { theP2 = P2Next; theP1-Next =P2;94         } the     } the } the 98 intMain () About { - Josephus t;101T.Set();102 t.creat ();103cout <<Endl;104 T.del (); the     return 0;106}

Code parsing

5-9 rows define the structure of the linked list, where Num is the initial number

23 rows of Tmp_n are responsible for recording the initial value of n

75 lines start looping, n+1 cycles, each time moving the P2 to the location of the person who needs to be excluded.

N=0 is the last cycle, at which point P1 and P2 are pointing to the last person, output num.

N!=0 removes the node that the P2 points to and begins the next loop.

The key to solving the Joseph ring problem is to get rid of the man with the Count M and start counting again.

The above solution cleverly uses the circular link list to solve the problem. Because the understanding of the linked list is not in-depth, so for the moment still can't think of a better solution.

Summarize

The key to solve the list problem is the construction of the list and other operations, and the list of problems only by imagination is impossible to solve, it is best to do their own drawing , so that all will become clear, the problem will be solved.

C + + uses circular link list to solve Joseph ring problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.