112 - Tree Summing

来源:互联网 发布:网速流量监控软件 编辑:程序博客网 时间:2024/06/05 15:22

方法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;}