UVA - 699 The Falling Leaves

題意:每一個結點都有一個水平位置,下標範圍最多80,左右子結點分佈在根結點左右各相距1個單位,從左到右輸出個各水平位置結點權值之和。先序輸入。ios

分析:邊輸入邊統計,注意清零和格式。ui

#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");
    }
}
相關文章
相關標籤/搜索