Data Structure practice -- monkey selects the king and the data structure Monkey King

Source: Internet
Author: User

Data Structure practice -- monkey selects the king and the data structure Monkey King

This article focuses on the basic series of online courses on data structures (2): practical projects of linear tables.

Project-monkey election king]
A group of monkeys numbered 1, 2, 3... M, this group of monkeys (m) sat around in 1-m order. The number starts from 1st and the number of monkeys to the nth is about to leave the circle. In this way, the monkey is the king until only the last monkey is left in the circle. Input m and n, and the number of monkeys that output as the king is.

Tip:
(1) linked list solution: a Circular Single-chain table can be used to represent this group of monkeys. There are two members in the structure of the node: one for saving the Monkey number, one for pointing to the next person, and the other for the node numbered m pointing to the node numbered 1, to form a ring chain. When the number is n, the node is deleted and continues until there is only one node.
(2) Use a structure array to represent the loop chain: Set a member in the struct to indicate whether the corresponding monkey has been eliminated. From the number of records not eliminated by the first person, the number of records in the structure is changed to 0, indicating that the monkey has been eliminated. When the number of m elements in the array is reached, the number starts from the first number again, so that the cyclic count continues until the number of m elements is eliminated.
(3) This is a classic problem in Computer Science. many practical problems can be abstracted to this model. If you are interested, search for "Joseph's question ".

[Reference solution (C ++ implementation)]

# Include <iostream> using namespace std; struct Monkey {int num; // Monkey ID struct Monkey * next; // next Monkey}; int main () {int m, n, I, j, king; Monkey * head, * p1, * p2; cin> m> n; if (n = 1) {king = m;} else {// create a Monkey circle p1 = p2 = new Monkey; head = p1; head-> num = 1; for (I = 1, p1-> num = 1; I <m; I ++) // The Rest Of The m-1 monkeys {p1 = new Monkey; // p1 is the newly added p1-> num = I + 1; p2-> next = p1; p2 = p1; // p2 is always the last one} p2-> next = head; // The Last One points to the first one and becomes a circle. // The number of p1 = head starts below; for (I = 1; I <m; I ++) // cyclically M-1 th to eliminate the 1 th monkey. {// starting from p1, n-1 only finds n, for (j = 1; j <n-1; j ++) // actually finds n-1 first, the next one will be the p1 = p1-> next; // circled, and may start from the first number. If it has not been eliminated, // It will be found, p2 = p1-> next; // p2 will be deleted // cout <"no." <I <"" <p2-> num <endl; // you can observe the intermediate result p1-> next = p2-> next; // p2 is "overhead" p1 = p2-> next; // delete p2, the new starting point of the next round of data count; // discard nodes not in the linked list} king = p1-> num; delete p1 ;}cout <king <endl; return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.