Array-11. Monkey selection King (20)

Source: Internet
Author: User

Array-11. Monkey selection King (20) time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Xu Jianchun (Zhejiang University)

A group of monkeys will choose the new Monkey King. The new Monkey King's choice is: Let n only the candidate monkeys in a circle, from a position from a sequence numbered 1-n. Starting from the 1th number, each round from 1 to report 3, where the report 3 of the monkeys are out of the circle, and then from the next close to a monkey start the same count. So constantly circulating, the last one left is chosen as the Monkey King. What is the original number of monkeys elected Monkey King?

Input format:

The input gives a positive integer n (<=1000) in a row.

Output format:

Prints the number of the selected monkey in a row.

Input Sample:
11
Sample output:
7



The following ideas are wrong, beginning with the erase, but found that the iterator will be pumped//later with the turn to 3 of the element into 0, found also not, because it will involve the repetition into a 3 case//#include <iostream>//#include <         list>//using namespace std;//int sum (list<int> num)//{/int s = 0;//for (auto I:num)//{//     if (i = = 0)//++s;//}//return s;//}//int main ()//{//int n;//cin >> n;// List<int> num;//for (int i = 1; I <= N; ++i)//{//Num.push_back (i);//}//int flag = 0;             Auto it = Num.begin ();//while (SUM (num)! = N-1)//{//if (it = = Num.end ())///{//             it = Num.begin ();//}//++flag;//if (flag = = 3)//{//*it = 0;//   Flag = 0;//}//++it;         Next iter////cout << *it << endl;//}//for (auto I:num)//{//if (I! = 0)// {//cout << i << endl;//}//}//return 0;//}

Finally change the erase, you can use the erase!

#include <iostream> #include <list>using namespace Std;int main () {    int N;    Cin >> N;    list<int> num;    for (int i = 1; I <= N; ++i)    {        num.push_back (i);    }    int flag = 0;        Symbol of the    auto it = Num.begin ();    while (Num.size () > 1)    {        ++flag;        if (flag = = 3)        {            it = num.erase (it);//Note It is invalid after deletion and cannot go to ++!!!            //erase will return the next pointer to the deleted element            flag = 0;        }        if (flag! = 0)      //At first there is a mistake, due to the deletion of the save operation, resulting in the deletion of the words will automatically move to the next            ++it;   Next ITER        if (it = = Num.end ())        {            it = Num.begin ();        }    }    cout << *num.begin () << Endl;    return 0;}


Array-11. Monkey selection King (20)

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.