Monkey fall time limit: 3000 MS | memory limit: 65535 KB
Difficulty: 3
-
Description
-
There is a binary tree with a maximum depth of D, and the depth of all leaves is the same. The numbers of all nodes from left to right are 1, 2, 3,..., and 2 minus 1 to the power of D. Put a monkey at Node 1 and it will run down. Each inner node has a switch that is initially closed. When a monkey runs to a switch, its status changes. When it reaches an inner node, if the switch is closed, the monkey moves to the left. Otherwise, the monkey moves to the right until it reaches the leaf node.
-
Some monkeys start to run down from node 1. Where will the last monkey go?
-
-
Input
-
-
Enter the depth D of the binary tree leaves, and the number of monkeys I. Assume that I cannot exceed the number of leaves of the entire tree. D <= 20. The end ends with 0.
-
-
Output
-
Output the leaf number of the monkey I.
-
-
Sample Input
-
4 23 40 0
-
-
Sample output
-
127
Code:
# Include "stdio. h "int main () {int d, I, j, num = 1; while (scanf (" % d ", & d, & I ), d & I) {num = 1; for (j = 1; j <d; j ++) {if (I % 2 = 0) num = num * 2 + 1; // turn an even number to the right. Turn an odd number to the left. else num = num * 2; I = (I + 1)/2; // avoid I = 0} printf ("% d \ n", num);} return 0 ;}