分析:看起来还是需要递归建树,做完了uva839后,这题的建树就没问题了,关键是怎么存储每一列的值呢?
链表?也太麻烦了。。。
看了刘大爷这部分的处理,从数组中间开始赋值,然后左右递归处理。豁然开朗。
由题意col <= 80,左边和右边列数n<=40个,所以最大值设为100就够了。
ps:题目说用空格把output分开,结果我在最后一个值后面也加了空格,wa了多次,醉了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100;
int ans[maxn];
void build(int index)
{
int v;
cin >> v;
if(v != -1){
ans[index] += v;
build(index-1);
build(index+1);
}
return;
}
bool indata()
{
int v;
cin >> v;
if(v == -1) return false;
memset(ans,0,sizeof(ans));
int tmp = maxn/2;
ans[tmp] = v;
build(tmp-1);
build(tmp+1);
return true;
}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// #endif
int k = 1;
while(indata()) {
int i = 0;
printf("Case %d:\n",k++);
while(ans[i] == 0) i++;
printf("%d",ans[i++]);
while(ans[i++] != 0) printf(" %d",ans[i-1]);
printf("\n\n");
}
return 0;
}