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
Import Java.util.scanner;public class Main {public static void Main (string[] args) { Scanner cin = new Scanner (Sy stem.in); int n = cin.nextint (); int[] Monkey = new Int[n]; for (int i = 0; i < n; i++) { Monkey[i] = i + 1; } for (int i = n-1; I >= 0, i--) {for (int j = 0; J < 3; J + +) { int temp = monkey[0]; for (int k = 0; k < i; k++) { Monkey[k] = monkey[k + 1]; } Monkey[i] = temp; } } System.out.println (Monkey[0]);} }
Array-11. Monkey selection King (20)