UVA - 712 S-Tree
来源:互联网 发布:林心如律师发声明知乎 编辑:程序博客网 时间:2024/06/05 15:06
题目大意:满二叉树,节点值为 0 或 1。给出一个 n 表示该二叉树的深度,第二行 x3,x1,x2 表示从根节点开始每一层的值,第三行表示最后一层即叶子的值。接着给出要查找的次数 m,以下 m 行每行分别表示 x1,x2,x3 的值。有一颗小球从根节点出发,遇到 0 左走 1 右走,输出最后到达的叶子节点的值。
解题思路:根据满二叉树做孩子为 2k,右孩子 2K+1 的特点解决。创建一个数组 leaf 每个下标对应二叉树节点的编号,读入叶子节点将每个值放入对应的编号中。读取每一层的值计算最后到达的编号,直接从数组中读取对应的值。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctype.h>using namespace std;char tmp[100];int root[10];char leaf[1000];char order[10];int cnt = 0;int main() { int n; char c; while (scanf("%d\n", &n) != EOF && n) { printf("S-Tree #%d:\n", ++cnt); gets(tmp); gets(leaf); int tag = 0; for (int i = 0; tag < n; i++) if (tmp[i] >= '0' && tmp[i] <= '9') root[tag++] = tmp[i]-'0'; tag = (2<<n)-1; for (int i = strlen(leaf)-1; i >= 0; i--) leaf[tag--] = leaf[i]; int m; scanf("%d\n", &m); while (m--) { gets(order); int sum = 1; for (int i = 0; i < n; i++) { if (order[root[i]-1] == '0') sum = sum*2; else sum = sum * 2 + 1; } printf("%c", leaf[sum]); } printf("\n\n"); }return 0;}
0 0
- UVA - 712 S-Tree
- UVA-712 S-Tree
- UVA 712 S-Tree
- uva 712 S-Tree (S树)模拟
- 【UVA 712 S-Tree】& 二叉树
- UVA Tree's a Crowd
- S-Tree解题报告UVa 712二叉树/二分搜索
- UVa OJ 152-Tree's a Crowd
- uva 152 Tree's a Crowd
- UVA 152 - Tree's a Crowd
- UVa 152 - Tree's a Crowd
- uva 152 - Tree's a Crowd
- UVA-152- Tree's a Crowd
- UVA 152 - Tree's a Crowd
- UVa 152 - Tree's a Crowd
- uva 152 Tree's a Crowd
- UVa 152 - Tree's a Crowd
- UVA 152 Tree's a Crowd
- POJ 2388 排序
- 动画加载ListView
- css选择器与jquery选择器
- 良好的代码规范 -- 缩进
- strutus2中获得jsp页面的元素值
- UVA - 712 S-Tree
- 欢迎使用CSDN-markdown编辑器
- ROS by Example 第2卷 -- 概括
- UVA - 327 Evaluating Simple C Expressions
- PullToRefresh简单用法
- 安卓5.0 多用户之访客模式的说明文档
- iOS 给TableView添加Footer或Header
- UVA - 839 Not so Mobile
- Linux系统下载安装SVN客户端并进行配置