题目描述:
题目思路:
1.直接用数组模拟二叉树下落过程
//超时
#include <iostream> #include <cstring> using namespace std; const int maxn = 20; int s[1<<maxn] ; int main(int argc, char *argv[]) { int t; scanf("%d",&t) ; while(t--) { int d,n; scanf("%d%d",&d,&n) ; memset(s,0,sizeof(s)); int k,m = (1<<d) - 1;//最大节点的下标 for(int i = 0;i < n;i++) { k = 1; while(true) { s[k] = !s[k] ; k = s[k] ? k*2 : k*2+1 ; if(k > m) break ; } } cout << k/2 << endl ; } return 0; }
//紫书提供了另一个思路
#include<cstdio> int main() { int T, D, I; scanf("%d", &T); while(T--) { scanf("%d%d", &D, &I); int k = 1; for(int i = 0; i < D-1; i++) if(I%2) { k = k*2; I = (I+1)/2; } else { k = k*2+1; I /= 2; } printf("%d ", k); } return 0; }