699 - The Falling Leaves

/*
简单题:
不需要建树,只需遍历一个记清状态
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
using namespace std;

int result[200];
int p[10010];
int len;

void traverse(int &pos,int loc)
{
	if(pos<len && p[pos]!=-1)
	{
		result[loc]+=p[pos];
		traverse(++pos,loc-1);
		traverse(++pos,loc+1);
	}
}

int main()
{
	/*
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
	*/
	string s;
	int cas=1;
	while(1)
	{
		getline(cin,s);
		if(s=="-1")
			break;
		else if(s[0]=='-' && s[1]=='1')
			continue;
		else
		{
			istringstream in(s);
			len=0;
			int a;
			while(in>>a)
				p[len++]=a;
			printf("Case %d:\n",cas++);
			memset(result,0,sizeof(result));
			int pos=0;
			traverse(pos,100);
			int i=0;
			while(result[i]==0) i++;
			printf("%d",result[i++]);
			for(;result[i]!=0;i++)
				printf(" %d",result[i]);
			//Follow the output for each case by a blank line.
			printf("\n\n");
			
		}
	}
	return 0;
}


你可能感兴趣的:(699 - The Falling Leaves)