UVA 699 - The Falling Leaves

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int a[210][210];
int sum[210];
void creat(int dep, int mid)
{
    int x;
    scanf("%d", &x);
    if(x == -1) return;
    else {
        a[dep][mid] += x;
        sum[mid] += x;
        creat(dep+1, mid-1);
        creat(dep+1, mid+1);
    }
}
int main()
{
    int root, kase = 0;
    while(scanf("%d", &root), root != -1)
    {
        memset(sum, 0, sizeof(sum));
        memset(a, 0, sizeof(a));
        int mid = 100;
        a[0][mid] = root;
        sum[mid] += root;
        creat(1, mid-1);
        creat(1, mid+1);
        printf("Case %d:\n", ++kase);
        bool f = true;
        for(int i = 0; i < 200; ++i)
        {
            if(sum[i] != 0)
            {
                if(!f) printf(" "), printf("%d", sum[i]);
                else printf("%d", sum[i]), f = false;
            }
        }
        printf("\n\n");
    }
    return 0;
}

阅读更多
个人分类: tree 数据结构
上一篇uva 839 - Not so Mobile
下一篇uva 327 - Evaluating Simple C Expressions
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭