| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
两个注意点!附0MS代码.0ms,这题要注意两点,一是节点数据可能为负,二是输入时使用cin.unget()可让输入流退回上一格,神奇啊!
http://acm.pku.edu.cn/JudgeOnline/problem?id=1145
#include <iostream>
#define MAX_N 10000
using namespace std;
struct node
{
int sum;
node *lchild;
node *rchild;
};
node freeNode[MAX_N];
int p,target;
bool Done;
bool getNext(int &data,char &ch)
{
data=-1;
ch=' ';
while(ch==' ')
cin>>ch;
if(ch>='0'&&ch<='9'||ch=='-')
{
cin.unget();
cin>>data;
return false;
}
return true;
}
node *build(int sum)
{
char ch;
int data;
bool isCh;
isCh=getNext(data,ch);
isCh=getNext(data,ch);
if(isCh&&ch==')'||ch=='\n')
return NULL;
node *newNode=&freeNode[p++];
newNode->sum=data+sum;
newNode->lchild=build(newNode->sum);
newNode->rchild=build(newNode->sum);
if(newNode->lchild==NULL&&newNode->rchild==NULL&&newNode->sum==target)
Done=true;
isCh=getNext(data,ch);
return newNode;
}
int main()
{
while(scanf("%d",&target)!=EOF)
{
p=0,Done=false;
node *root=build(0);
if(Done)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator