菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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  好吧,就是菜菜  阅读(158)  评论(0编辑  收藏  举报