例题6-10 UVa699 The Falling Leaves(二叉树)

题意:

看白书

要点:

构造一个数组,从中间的下标开始,向左就-1,向右就+1,够简单的


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 100005
int sum[maxn];

bool build(int p)
{
	int v;
	scanf("%d", &v);
	if (v == -1)
		return false;
	sum[p] += v;
	build(p - 1);
	build(p + 1);
	return true;
}
int main()
{
	int kase = 0;
	while (build(maxn / 2))
	{
		int p = 0;	
		while (sum[p] == 0)
			p++;
		printf("Case %d:\n%d", ++kase,sum[p++]);
		while (sum[p] != 0)
			printf(" %d", sum[p++]);
		printf("\n\n");
		memset(sum, 0, sizeof(sum));
	}
	return 0;
}


posted @ 2016-02-12 18:08  seasonal  阅读(82)  评论(0编辑  收藏  举报