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)