UVA - 699 The Falling Leaves
题意:每个结点都有一个水平位置,下标范围最多80,左右子结点分布在根结点左右各相距1个单位,从左到右输出个各水平位置结点权值之和。先序输入。
分析:边输入边统计,注意清零和格式。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 200 + 10;//因为可能根结点在最左列或最右列 const int MAXT = 1000000 + 10; using namespace std; int sum[MAXN];//每列的叶子总数 void build_tree(int id){ int x; scanf("%d", &x); if(x != -1){ sum[id] += x; build_tree(id - 1); build_tree(id + 1); } } int main(){ int n; int cnt = 0; while(scanf("%d", &n) == 1){ if(n == -1) return 0; memset(sum, 0, sizeof sum); printf("Case %d:\n", ++cnt); int id = MAXN / 2;//根结点所在列 sum[id] += n; build_tree(id - 1); build_tree(id + 1); bool ok = true; for(int i = 0; i < MAXN; ++i){ if(sum[i]){ if(ok) ok = false; else printf(" "); printf("%d", sum[i]); } } printf("\n\n"); } }