UVa 679 Dropping Balls (例题 6-6)

传送门:https://uva.onlinejudge.org/external/6/p679.pdf

题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D <= 20), 根结点为1(节点从1开始到2D-1),开关为关闭向左子结点走,否则往右子结点走,每到一个结点改变该结点开关状态。问第 I 颗球落在哪。

当 I 是奇数时, 它是往当前结点的左子结点走的第 (I + 1) / 2 颗球;

当 I 是偶数时, 它是往当前结点的右子结点走的第 I / 2 颗球;

思路挺有趣的(又一次虐我智商)  虽然好像可以暴力..........

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int t;
 5     cin >> t;
 6     while(t--){
 7         int n, m;
 8         cin >> n >> m;
 9         int i = 1;
10         while(i < (1<<n)){
11             if(m & 1){
12                 i <<= 1;
13                 ++m;
14                 m >>= 1;
15             }
16             else{
17                 i <<= 1;
18                 ++i;
19                 m >>= 1;
20             }
21         }
22         cout << (i >> 1) << endl;
23     }
24     cin >> t;
25     return 0;
26 }
原文地址:https://www.cnblogs.com/book-book/p/5335401.html