uva 112 - Tree Summing
来源:互联网 发布:mac电脑怎么退出全屏 编辑:程序博客网 时间:2024/06/04 19:09
数据结构。
给一颗树,求根到各个最下面的结点和。有则yes,无则no。
主要是输入的 时候不是一行,有空格和回车。
我用栈把树的结点值求存入,然后到最后判断,判断完拿出来,不过貌似处理蛮麻烦的。
#include<cstdio>#include<cstring>#include<stack>using namespace std;int main(){ //freopen("in.txt","r",stdin); int n; while(~scanf("%d",&n)){ stack<char>csta; stack<int>ista; while(!ista.empty()) ista.pop(); int temp=0,cnt=0,ans=0; bool ok=false; bool solve=false; while(1){ char c=getchar(); if(c<='9' && c>='0'||c=='-'){ temp=0; bool is=false; if(c=='-') { is = true; while(1){ c = getchar(); if(c==' '|| c=='\n') continue; else break; } } while(1){ temp = temp*10 + (c-48); c = getchar(); if(!(c<='9'&&c>='0')) break; } if(is) temp = -temp; ans += temp; ista.push(temp); cnt=0; } if(c==' ' || c=='\n') continue; if(c=='('){ ok=false; csta.push(c); } else if(c==')'){ if(!ok) {cnt++;csta.pop();ok=true;} else{ temp = ista.top(); ista.pop(); ans -= temp; csta.pop(); cnt=0; } } if(cnt==2){ if(ans==n) solve=true; cnt=0;ok=true; } if(csta.empty()) break; } if(solve) printf("yes\n"); else printf("no\n"); } return 0;}
下面某一大神的代码,现在看不懂~,以后看下……和加强版测试数据
22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
20 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()()))))
10 (3
(2 (4 () () )
(8 () () ) )
(1 (6 () () )
(4 () () ) ) )
5 ()
0 ()
5 (5 () ())
5 ( 5 () () )
5 (1 (3 () ()) (4 () ()))
5 (18 ( - 13 ( ) ( ))())
0 (1 ()(-2 () (1()()) ) )
2 (1 () (1 () (1 () () ) ) )
10 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )
10 (5 () (5 () (5 () (5 ( 3 () () ) (4 () () ) ) ) ) )
20 (5 () (5 () (5 () (5 () (4 () () ) ) ) ) )
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>using namespace std;int flag;int t_sum(int n,int sum){ int data; char c; cin>>c; //输入左括号 cin>>data; if(!(cin==0)) { sum+=data; int ok1=t_sum(n,sum); //左树 int ok2=t_sum(n,sum); //右树 if(!ok1&&!ok2&&!flag) //如果左右两树都为空,即是叶子,且flag还是0,那么对总和sum与n进行比较 if(sum==n) //若相等则将flag赋为1 flag=1; cin>>c; //输入右括号 return 1; } else { cin.clear(); //清除错误 cin>>c; return 0; }}int main(){ //freopen("sample.txt","r",stdin); int n; while(cin>>n) { flag=0; t_sum(n,0); cout<<(flag?"yes":"no")<<endl; } 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
- NC57logo修改办法
- 使用startForeground让android服务前台运行
- usaco 2008 January Telephone Lines 架设电话线 题解
- HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either w
- j2ee的学习顺序
- uva 112 - Tree Summing
- Qt 表格中插入QCheckBox并居中
- java中的事物控制:Statement和PreparedStatement
- Filter技术的应用(转)
- 2010华南农业大学校赛-Join a guild
- C++ 调用内置数据类型的构造函数
- Log4j 的初始化
- Android Bitmap图片的处理
- Pat(Advanced Level)Practice--1065(A+B and C (64bit))