NYOJ63 monkey whereabouts
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
-
# include
# include
# define TRUE 1int i;int fact(int i, int n,int *a){if (2*i > n && 2*i +1 > n){return i;}else{if (a[i] == 0 ){a[i] = 1;i = 2*i;}else{a[i] = 0;i = 2*i +1;}fact(i,n, a);}}int main(void){int n,m,s;while (scanf("%d%d", &n,&m) && (n != 0 || m != 0)){int a[100000] = {0};while (m--){s = fact(1,pow(2,n) - 1,a);}printf("%d\n", s);}return 0;}
-
I am not talented, just want to solve the problem with recursion, look at the code of the following great gods
-
#include
02.using namespace std;03. 04.int main()05.{06.int d,i,k;07.while(cin>>d>>i && (d+i) !=0)08.{09.k=1;10.for (int j=0;j
11.
if
(i%2) {k=k*2;i=(i+1)/2;}
12.
else
{k=k*2+1;i /=2;}
13.
cout< 14. 15. } 16. }
Copy the Translation results to Google