Data structure Practice--The monkey Choose King

Source: Internet
Author: User

This paper is aimed at the data structure Basic Series Network course (2): Linear table Practice project.

"Project-Monkey King"
A group of monkeys, numbering is three-way ... m, the monkeys (m) sit around in the order of 1-m. Starting from the 1th number, every number to Nth, the monkey will leave this circle, so in turn, until the last monkey left in the circle, the monkey is king. Enter M and N, the number of monkeys that output as king.

(1) Chain list solution: You can use a circular single linked list to represent the group of monkeys. There are two members in the struct that represents the node: A number that holds the monkey, a pointer to the next person, a node numbered m, and a node numbered 1 to form a chain of rings. When you count to Nth, the node is deleted and continues to count until there is only one node.
(2) using an array of structures to represent the loop chain: a member of the structure is set up to indicate whether the corresponding monkey has been eliminated. From the first person who has not been eliminated, each number to n, the structure of the mark changed to 0, indicating that the monkey has been eliminated. When you count the number of M elements in the array, re-start from the first number, so that the loop counts until M-1 are eliminated.
(3) This problem is a classical problem in computer science, and many practical problems can be abstracted into this model. Interested students please search for "Joseph question".

[Reference solution (C + + implementation)]

#include <iostream>using namespace STD;structmonkey{intNum//Monkey's number    structMonkey *next;//Next Monkey};intMain () {intm,n,i,j,king; Monkey *head, *P1,*P2;Cin>>m>>n;if(n==1) {king=m; }Else{//Create a circle around the monkeyP1=p2=NewMonkey;        head = p1; head->num=1; for(i=1, p1->num=1; i<m; i++)//The rest m-1 only Monkeys{p1=NewMonkey;//P1 is a new addition.p1->num=i+1;            p2->next=p1; P2=P1;//p2 always on one.} p2->next=head;//The last one points to the first one and becomes a circle        ///below to start countingP1=head; for(i=1; i<m; i++)//Cycle m-1 times, eliminate m-1 only monkeys{//Starting from P1, the number of n-1 will only find the nth.             for(j=1; j<n-1; J + +)//Actually find the first n-1, the next one will be eliminated.p1=p1->next;//circled, may start again from the first number, if not yet eliminated            //Found,p2=p1->next;//p2 will be removed            //cout<< "First" <<i<< "Turn-out" <<p2->num<<endl; You can observe the intermediate results in this wayp1->next=p2->next;//P2 was "overhead."p1=p2->next;New starting point for the next round of counting            DeleteP2;//Discard nodes that are not in the list} king=p1->num;DeleteP1; }cout<<king<<endl;return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure Practice--The monkey Choose King

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: 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.