Array-11. Monkey selection King (20)

Source: Internet
Author: User

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

1#include <iostream>2#include <stdio.h>3#include <math.h>4#include <string.h>5#include <stdlib.h>6 7 using namespace:: STD;8 9 intMain () {Ten       intN; Onescanf"%d",&n); A       inta[ +]={0}; -        -        the       intI=0, j=0, flag=0, count=N; -        while(flag==0) -       { -            +           if(a[j]==0)//whether to survive -           { +             if(i%3==2)//alive, and be chosen A             { ata[j]=1; -J= (j+1)%N; -count--; -i++; -             } -             Else in             { -i++; toJ= (j+1)%N; +             } -                  the           } *           Else $         {Panax NotoginsengJ= (j+1)%N;  -           } the            +           if(count==1) A           { the                for(intk=0; k<n;k++) +               { -                   if(a[k]==0) $                   { $printf"%d", K +1); -                   } -               } theflag=1; -           }Wuyi       } the       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.