UVA699-落叶-二叉树

本文提供了一种解决UVA699-落叶问题的方法,通过构建二叉树来处理输入数据,并使用递归算法计算每个节点的权值,最终输出整棵树的结构。

1:用一根数轴,根结点是坐标index,左结点-1,右结点+1

还有一个0ms的不知道怎么过的

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<sstream>
using namespace std;
void caculate(int* tree, int index, int* min, int*max)
{
	int s;
	cin >> s;
	if (s != -1)
	{
		tree[index - 1] += s;
		if ((index - 1) < *min)
		{
			*min = index-1;
		}
		caculate(tree, index - 1, min, max);
	}
	cin >> s;
	if (s != -1)
	{
		tree[index + 1] += s;
		if ((index +1) > *max)
			{
				*max = index+1;
			}
		caculate(tree, index + 1, min, max);
	}
}
int main()
{
	freopen("d:\\1.txt", "r", stdin);
	int MAXN = 10000;
	int number = 1;
	while (cin)
	{
		int MIDDLE = 5000;
		int total[MAXN];
		memset(total, 0, sizeof(int) * MAXN);
		int s;
		cin >> s;
		if (s == -1)
			return 0;
		else
		{
			int max = MIDDLE;
			int min = MIDDLE;
			total[MIDDLE] += s;
			caculate(total, MIDDLE, &min, &max);
			cout << "Case " << number << ":" << endl;
			for (int i = min; i <= max; i++)
				if (i == min)
					cout << total[i];
				else
					cout << " " << total[i];
			cout << endl<<endl;
		}
		number++;
	}
}

  

posted on 2017-05-10 16:34 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6836673.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值