UVa699 The Falling Leaves
2016-03-09 16:09
232 查看
// UVa699 The Falling Leaves // Rujia Liu // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位。从左向右输出每个水平位置的所有结点的权值之和。按照递归方式输入,-1表示空树 // 算法:在“建树”的同时计算,无须真正的把树保存下来 #include<cstring> #include<iostream> using namespace std; const int maxn = 200; int sum[maxn]; // 输入并统计一棵子树,树根水平位置为p void build(int p) { int v; cin >> v; if(v == -1) return; // 空树 sum[p] += v; build(p - 1); build(p + 1); } // 边读入边统计 bool init() { int v; cin >> v; if(v == -1) return false; memset(sum, 0, sizeof(sum)); int pos = maxn/2; // 树根的水平位置 sum[pos] = v; build(pos - 1); // 左子树 build(pos + 1); // 右子树 return true; } int main() { int kase = 0; while(init()) { int p = 0; while(sum[p] == 0) p++; // 找最左边的叶子 // 开始输出。因为要避免行末多余空格,所以稍微麻烦一点 cout << "Case " << ++kase << ":\n" << sum[p++]; while(sum[p] != 0) { cout << " " << sum[p]; p++; } cout << "\n\n"; } return 0; }
相关文章推荐
- hvr的license过期时的提示
- 专硕还是学硕,用人单位会区别对待吗?
- RunTime交换两个方法,可以根据特定条件来改变.
- IMAP与POP3的区别
- VS 平台下 OpenGL 实现地球、月球、太阳运动
- cf #345div1 B ----------div2 D
- psync scheduled to be closed ASAP for overcoming of output buffer limits
- android 6.0权限全面详细分析和解决方案
- 朋友遇到过的t厂面试题
- 七层协议与网络配置
- Linux下添加php的zip模块
- swiper
- webstorm+nodejs+JetBrains IDE Support+chrome打造前端开发神器
- 动态规划 HDU 1176
- Java学习篇之---Eclipse中运行python程序
- Fling!
- MySQL常用引擎详解
- UITextField输入内容时闪烁,输入内容位置向下偏移
- 总结的JS数据类型判定(非常全面)
- SOA架构设计经验分享—架构、职责、数据一致性