题目链接:https://vjudge.net/problem/UVA-699
今天开始写紫书,刷完紫书刷白书!看到一个大佬的博客,去年省赛打铁刷了半年白书今年拿金…我????
题意就是构造一棵树,先输入父节点的值再输入依次左边、右边的值,递归构建就好。不得不说刘汝佳的代码写的是真简洁。
还有个坑就是VJ上第二个样例有两行,我一直以为下面是一行-1开头的数据,半天出不来CASE2的结果,DEBUG了半小时发现看错了??
/*
* @Author: SamsonHo
* @Date: 2018-09-11-15.30.40
* @URL:https://vjudge.net/problem/UVA-699
*/
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
int sum[200];
void build(int p)
{
int v;
scanf("%d",&v);
if(v == -1) return;
sum[p] += v;
build(p-1); build(p+1);
}
bool init()
{
int v;
scanf("%d",&v);
if(v == -1) return false;
memset(sum,0,sizeof sum);
int pos = 200/2;
sum[pos] = v;
build(pos-1); build(pos+1);
return true;
}
int main(void)
{
int kase = 0;
while(init())
{
int p = 0;
while(0 == sum[p]) ++p;
printf("Case %d:\n%d",++kase,sum[p++]);
while(sum[p])
printf(" %d",sum[p++]);
puts("\n");
}
}