您的位置:首页 > 其它

uva 699

2015-05-20 11:54 232 查看
用一个int 变量 p 记录一下树中节点的位置,边输入就可以建树。

用flag 变量记录是否初始化。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int maxn = 10000;
int tree[maxn];
bool flag = true;

bool input(int p){
int n;
cin>>n;
if(n==-1) return false;
if(flag == true){
memset(tree,0,sizeof(tree));
tree[p] = n;
flag = false;
}
else {
tree[p] += n;
}
input(p-1);input(p+1);
return true;
}

int main(){
int kase = 0;
while(input(maxn/2)){
int p = 0;
while(tree[p] == 0) p++;
cout<<"Case "<<++kase <<":\n"<<tree[p++];
while(tree[p] != 0) cout<<" "<<tree[p++];
cout<<"\n\n";
flag = true;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: