112 - Tree Summing

方法1://充分的利用了cin对char,int 类型不读回车和空格的特点

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int pan=0;
bool judge(int sum,int bi)
{
   char c; int a;
    cin>>c;
    if((cin>>a)==0)
    {   cin.clear();//清除错误的状态
        char cc;
        cin>>cc;
        return false;
    }
    if(!judge(sum+a,bi)&!judge(sum+a,bi))
    {
        if((sum+a)==bi)pan=1;
    }
    char cc;
    cin>>cc;
    return true;

}
int main()
{
    int n;
    while(cin>>n)
    {   
        pan=0;
        judge(0,n);
        cout<<(pan?"yes\n":"no\n");
    }
    return 0;
}

方法2 //建树再bfs进行计算这个方法在uva上提交竟然比方法一用时少很纳闷。。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
char ch[10010];int ci;int ok;int ii;
void init()
{   ci=0;ok=0;ii=0;
    char c;
    int zc=0,yc=0;
    int sd=0;
    for(;;)
    {
        c=getchar();
        if(c=='('){sd=1;}
        if(sd==0)continue;
        if(c=='(')
        {
            ch[ci++]=c;
            ++zc;
        }
        if(c==')')
        {
            ch[ci++]=c;
            ++yc;
        }
        if(c=='-')
        {
            ch[ci++]=c;
        }
        if(c>='0'&&c<='9')
        {
            ch[ci++]=c;
        }
        if(zc==yc)break;
    }
    ch[ci]='\0';
    return ;
}
typedef struct Tnode
{
    int v;
    Tnode *left,*right;
}node;
node *newnode()
{
    node *n=(node*)malloc(sizeof(node));
    if(n)
    {
        n->left=NULL;
        n->right=NULL;
    }
    return n;
}

void buildtree(node * &root)
{   ii++;//cout<<"ii"<<ii<<endl;
   // system("pause");
    if(ch[ii]==')')
        {++ii;return ;}
    if(root==NULL)root=newnode();
    int a=0;
    if(ch[ii]=='-')
    {   ++ii;
        for(;;++ii)
        {
            if(ch[ii]<='9'&&ch[ii]>='0')
            {
                a=ch[ii]-'0'+a*10;
            }
            else
            {
                a=(-1)*a;
                break;
            }
        }
    }
    else
    {
        for(;;++ii)
        {
            if(ch[ii]<='9'&&ch[ii]>='0')
            {
                a=ch[ii]-'0'+a*10;
            }
            else
            {
                break;
            }
        }
    }
    root->v=a;//cout<<a<<"ee\n";
    //system("pause");
    buildtree(root->left);
    //system("pause");
    buildtree(root->right);
    ++ii;
    return;
}
bool   bfs(node *&root ,int sum,int bi)
{
    if(root==NULL)return 0;
    if(!bfs(root->left,sum+root->v,bi)&!bfs(root->right,sum+root->v,bi))
    {
        if(root->v+sum==bi)ok=1;//cout<<sum+root->v<<endl;
    }
    return 1;
}
void remove_tree(node*& root)
{
    if(root==NULL)return ;
    remove_tree(root->left);
    remove_tree(root->right);
    free(root);
}
int main()
{   //freopen("in.txt","r",stdin);
    int n;
    while(~scanf("%d",&n))
    {
        init();
        int i;

        if(strlen(ch)==2){printf("no\n");continue;}
       // system("pause");
//        for(i=0;i<ci;++i)
//        printf("%c",ch[i]);
//        cout<<endl;
        node* root;
        root=newnode();
        buildtree(root);
//        int tou=0,wei=1;
//        node *aa[100];
//        aa[0]=root;
//        while(tou<wei)
//        {
//            printf("%d ",aa[tou]->v);
//            if(aa[tou]->left!=NULL)aa[wei++]=aa[tou]->left;
//            if(aa[tou]->right!=NULL)aa[wei++]=aa[tou]->right;
//            tou++;
//        }
        bfs(root,0,n);
        remove_tree(root);
        if(ok)printf("yes\n");
        else printf("no\n");
    }
    return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值