uva 112 - Tree Summing

数据结构。

给一颗树,求根到各个最下面的结点和。有则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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值