679 - Dropping Balls (UVA)
题目链接如下:
Online Judge
我的方法不是很直观....更简洁的方式在这里:UVa 679 - Dropping Balls_uva dropping balls-CSDN博客
我的代码如下:
#include <cstdio>
#include <cmath>
// #define debug
int l, d, k, loc, len;
int main(){
#ifdef debug
freopen("0.txt", "r", stdin);
freopen("1.txt", "w", stdout);
#endif
scanf("%d", &l);
while (l--){
scanf("%d %d", &d, &k);
loc = pow(2, d - 1);
len = loc / 2;
while (k){
if (k % 2 == 0){
loc += len;
}
if (k < 2){
break;
}
k = (k + 1) / 2;
len /= 2;
}
printf("%d\n", loc);
}
scanf("%d", &l);
#ifdef debug
fclose(stdin);
fclose(stdout);
#endif
return 0;
}