例题6-6 小球下落(Dropping Balls, UVa 679)

原题链接:https://vjudge.net/problem/UVA-679
分类:树
备注:完全二叉树编号

如果是初学树,通过这题可以了解到二叉树编号的一些性质。
通过奇偶性来判断遍历的路径对我来说是挺新鲜的二叉树的一个特点。

代码如下:

#include<cstdio>
using namespace std;
int main(void) {
	int l, depth, num, ans;
	scanf("%d", &l);
	while (l--) {
		scanf("%d %d", &depth, &num);
		ans = 1;
		for (int i = 1; i < depth; i++) {
			if (num % 2) ans = ans << 1;
			else ans = ans << 1 | 1;
			num = (num + 1) >> 1;
		}
		printf("%d\n", ans);
	}
	scanf("%d", &l);
	return 0;
}
发布了104 篇原创文章 · 获赞 97 · 访问量 4515

猜你喜欢

转载自blog.csdn.net/TK_wang_/article/details/105344079