UVA-112 Tree Summing
来源:互联网 发布:李世石人工智能围棋 编辑:程序博客网 时间:2024/05/20 23:24
2016.11.2
UVA - 112 Tree Summing
题目大意:给一个数和一棵树问是否有一叶子到根的值为该数。
解题思路:递归,若当前节点无孩子(叶子)则判断和是否为所需数字。结束后需将尾部反括号读入以免影响下一个样例。
#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
- Android学习笔记第三篇--多媒体
- NOIP2012国王游戏
- 分享 共赢 网易视频云助力第5届TOP100summit盛大召开
- go-restful 简单学习
- [斐波那契 数学技巧] 洛谷 P1306 斐波那契公约数
- UVA-112 Tree Summing
- linux 下python CGIHTTPServer 报错
- 教你彻底解决Eclipse自动补全变量名的问题
- Java程序如何正确打包并正常运行在Linux系统中
- oracle instance client imp emp
- 【深度学习】笔记7:如何将mean,binaryproto格式的均值文件转化为mean.npy格式的均值文件
- Caffe源码阅读笔记(1):Blob
- Linux C 数据库编程
- 机器学习&数据挖掘:特征选择之Filter : Focus Approach