nyist oj 63(二叉树)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=63

代码如下:

View Code
 1 #include <cstdio>

 2 #include <cstring>

 3 using namespace std;

 4 

 5 const int N = 20;

 6 int s[1 << N];

 7 

 8 int main()

 9 {

10     int d, r;

11     while(scanf("%d%d", &d, &r) != EOF)

12     {

13             memset(s, 0, sizeof(s));

14             int k, n = (1 << d)-1;

15             for(int j = 0; j < r; j++)

16             {

17                     k = 1; 

18                     for(; ;)

19                     {

20                           s[k] = !s[k];

21                           k = s[k] ? k*2 : k*2+1;

22                           if(k > n)

23                                break;      

24                     }        

25             }                    

26             printf("%d\n", k / 2);

27     }

28     return 0;    

29 }

更精简的代码如下:

View Code
 1 #include <cstdio>

 2 using namespace std;

 3 

 4 int main()

 5 {

 6     int d, r;

 7     while(scanf("%d%d", &d, &r) != EOF)

 8     {

 9          int k = 1;

10          for(int i = 0; i < d-1; i++)

11          {

12                  if(r % 2)

13                  {

14                       k = k*2;

15                       r = (r+1)/2;     

16                  }              

17                  else

18                  {

19                       k = k*2+1;

20                       r /= 2;    

21                  }

22          }   

23          printf("%d\n", k);                

24     }

25     return 0;    

26 }

你可能感兴趣的:(二叉树)