您的位置:首页 > 其它

uva_699_The Falling Leaves

2012-11-07 17:52 225 查看
无需建树,递归写入数组,然后就是答案输出。
#include <cstdio>
#include <cstring>

#define MAXN    1001

int rst[MAXN], val;

void recusion_set_rst(const int &idx)
{
scanf("%d", &val);
if( -1 != val ) {
rst[idx-1] += val;
recusion_set_rst(idx-1);
}
scanf("%d", &val);
if( -1 != val ) {
rst[idx+1] += val;
recusion_set_rst(idx+1);
}
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas(1), idx;
while( scanf("%d", &val) && (-1 != val) ) {
memset(rst, 0, sizeof(rst));
idx = MAXN>>1; rst[idx] += val;
recusion_set_rst(idx);
printf("Case %d:\n", cas ++); idx = 0;
for(int i = 0; i < MAXN; i ++) {
if( !rst[i] ) {
continue;
}
if( idx ) {
printf(" ");
}
else {
idx = 1;
}
printf("%d", rst[i]);
}
printf("\n\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: