假設今天有4層:
第一顆球會落在2進位的1000,也就是10進位的8的位置。
第二顆球會落在2進位的1100,也就是10進位的12的位置。
第三顆球會落在2進位的1010,也就是10進位的10的位置。
第四顆球會落在2進位的1110,也就是10進位的14的位置。
...以此類推。
各位會發現到答案其實就是2的(層數-1)次方加上2進位倒過來的(次數-1),
例如4層的第一顆球就是2^(4-1)+倒置(1-1)=8+0=8。
4層的第二顆球就是2^(4-1)+倒置(2-1)=8+倒置(001(2進位))=8+(100(2進位))=8+4=12,
這樣即可得解。
[C](0.052)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main() | |
{ | |
int N; | |
while( scanf( "%d", &N ) != EOF ) | |
{ | |
int i; | |
for( i = 0 ; i < N ; i++ ) | |
{ | |
int D, I; | |
scanf( "%d%d", &D, &I ); | |
I--; | |
int j, k; | |
int tree = 1 << (D-1); | |
int route = 0; | |
for( j = D-2 ; j >= 0 ; j-- ) | |
{ | |
route ^= (I % 2) << j; | |
I /= 2; | |
} | |
tree ^= route; | |
printf( "%d\n", tree ); | |
} | |
} | |
return 0; | |
} |
0 意見:
張貼留言