题目链接:http://acm.hust.edu.cn/vjudge/problem/19244

#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #include<functional> #define mod 1000000007 #define inf 0x3f3f3f3f #define pi acos(-1.0) using namespace std; typedef long long ll; const int N=300; const int maxn = 200; int sum[maxn]; // 输入并统计一棵子树,树根水平位置为p void build(int p) { int v; cin >> v; if(v == -1) return; // 空树 sum[p] += v; build(p - 1); build(p + 1); } // 边读入边统计 bool init() { int v; cin >> v; if(v == -1) return false; memset(sum, 0, sizeof(sum)); int pos = maxn/2; // 树根的水平位置 sum[pos] = v; build(pos - 1); // 左子树 build(pos + 1); // 右子树 return true; } int main() { int kase = 0; while(init()) { int p = 0; while(sum[p] == 0) p++; // 找最左边的叶子 // 开始输出。因为要避免行末多余空格,所以稍微麻烦一点 cout << "Case " << ++kase << ": " << sum[p++]; while(sum[p] != 0) { cout << " " << sum[p]; p++; } cout << " "; } return 0; }