The Falling Leaves 下落的树叶 UVA 699(二叉树)
2017-01-09 15:42
369 查看
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=10000;
int sum[maxn];
void buildtree(int t){
int temp;cin>>temp;
if(temp==-1)return ;
sum[t]+=temp;
buildtree(t-1);buildtree(t+1);
}
bool inti(){
int temp;cin>>temp;
if(temp==-1)return false; //空树
int in=maxn/2;
sum[in]+=temp; //整棵树的根的水平位置
buildtree(in-1);buildtree(in+1);
return true;
}
int main(){
memset(sum,0,sizeof(sum));
int count=0;
while(inti()){
cout<<"Case "<<++count<<":"<<endl;
int temp=0;
while(sum[temp]==0)temp++; //找到最左边的叶子
cout<<sum[temp];
while(sum[++temp]!=0)
cout<<" "<<sum[temp]; //最后一个数字后不能有空格
cout<<"\n\n";
memset(sum,0,sizeof(sum));
}
return 0;
}
#include<cstring>
using namespace std;
const int maxn=10000;
int sum[maxn];
void buildtree(int t){
int temp;cin>>temp;
if(temp==-1)return ;
sum[t]+=temp;
buildtree(t-1);buildtree(t+1);
}
bool inti(){
int temp;cin>>temp;
if(temp==-1)return false; //空树
int in=maxn/2;
sum[in]+=temp; //整棵树的根的水平位置
buildtree(in-1);buildtree(in+1);
return true;
}
int main(){
memset(sum,0,sizeof(sum));
int count=0;
while(inti()){
cout<<"Case "<<++count<<":"<<endl;
int temp=0;
while(sum[temp]==0)temp++; //找到最左边的叶子
cout<<sum[temp];
while(sum[++temp]!=0)
cout<<" "<<sum[temp]; //最后一个数字后不能有空格
cout<<"\n\n";
memset(sum,0,sizeof(sum));
}
return 0;
}
相关文章推荐
- 下落的树叶(The Falling Leaves, UVa 699)
- 例题6-10 下落的树叶(The Falling Leaves, UVa 699)
- UVA699-例题6-10 下落的树叶(The Falling Leaves,UVa 699)
- UVA - 699 The Falling Leaves 二叉树
- Uva-699 - The Falling Leaves(二叉树与递归)
- The Falling Leaves UVA - 699 二叉树 深搜
- UVA 699 The Falling Leaves(二叉树|DFS)
- UVa 699 The Falling Leaves (DFS遍历二叉树)
- UVA 699 The Falling Leaves(二叉树)
- UVA - 699 The Falling Leaves (给出前序遍历,构建二叉树)
- UVA 699 The Falling Leaves (二叉树水题)
- UVA 699 The Falling Leaves (递归先序建立二叉树)
- uva699 The Falling Leaves 二叉树
- UVA 699 The Falling Leaves (二叉树水题)
- uva 699 The Falling Leaves 二叉树
- uva 699 The Falling Leaves(建二叉树同时求和)
- UVa 699 (二叉树) The Falling Leaves
- uva 699 The Falling Leaves (二叉树)
- UVa699-The Falling Leaves(下落的叶子)
- UVA.699 The Falling Leaves (二叉树 思维题)