UVA - 112 Tree Summing
来源:互联网 发布:股权众筹系统源码 编辑:程序博客网 时间:2024/05/18 04:28
题目大意:给一个数和一棵树问是否有一叶子到根的值为该数。
解题思路:递归,若当前节点无孩子(叶子)则判断和是否为所需数字。结束后需将尾部反括号读入以免影响下一个样例。
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctype.h>using namespace std;int n;void read() { char c; c = getchar(); while (c != '(' && c != ')') c = getchar();}int dfs(int tot, int *leaf) { int tmp, tag = 0, l = 0, r = 0; read(); if (scanf("%d", &tmp)) { *leaf = 1; tot += tmp; if (dfs(tot, &l)) tag = 1; if (dfs(tot, &r)) tag = 1; if (!l && !r) if (n == tot) tag = 1; else tag = 0; } read(); return tag;}int main() { while (scanf("%d", &n) != EOF) { int t = 1; if (dfs(0, &t)) printf("yes\n"); else printf("no\n"); }return 0;}
0 0
- uva 112 Tree Summing
- uva 112 - Tree Summing
- uva 112 - Tree Summing
- UVa 112 - Tree Summing
- uva 112 - Tree Summing
- uva 112 Tree Summing
- UVA 112 Tree Summing
- UVa 112 - Tree Summing
- UVa 112 - Tree Summing
- UVa 112 - Tree Summing
- uva 112 - Tree Summing
- uva-112 Tree Summing
- UVA 112 Tree Summing
- UVA 112 Tree Summing
- UVa 112 Tree Summing
- UVa 112 Tree Summing
- UVA 112 Tree Summing
- uva 112 Tree Summing
- disable bootstrap
- Oracle 基本知识 内含 SQLplus命令,Oracle常用用户
- UVA-10655 Contemplation! Algebra (矩阵快速幂)
- (UDP协议的应用)简单的广播实现
- jquery ajax 向后台传递数组参数示例
- UVA - 112 Tree Summing
- HDU 5753 Permutation Bo
- River Hopscotch<poj3528>
- 【HDU】1025 - Constructing Roads In JGShining's Kingdom(LIS & 深坑)
- UVA - 548 Tree
- CodeForces 427A
- CodeForces 274A k-Multiple Free Set【思维】
- UVA - 297 Quadtrees
- 面向对象