例题6-10 UVA 699 The Falling Leaves
程序员文章站
2022-05-28 15:24:20
...
二叉树的递归输入
#include <bits/stdc++.h>
using namespace std;
int k = 1;
map<int, int> mp;
void build(int pos)
{
int v;
scanf("%d", &v);
if(v == -1) return;
mp[pos] += v;
build(pos - 1); build(pos + 1);
}
bool init()
{
int v;
mp.clear();
scanf("%d", &v);
if(v == -1) return false;
mp[0] += v;
build(-1); build(1);
return true;
}
int main()
{
while(init()){
printf("Case %d:\n", k++);
int t = 0;
for(map<int, int>::iterator it = mp.begin(); it != mp.end(); it++){
if(t++) printf(" ");
printf("%d", it->second);
}
printf("\n\n");
}
return 0;
}