uva_699-The Falling Leaves
[cpp]
/**题目大意:先序建立二叉树,求同一垂直线上叶子节点之和。
*模拟先序建立二叉树过程,用数组保存值,数组下标代表垂直坐标求解
*/
#include <cstdio> www.zzzyk.com
#include <cstring>
using namespace std;
struct Node {
int v, pos;
Node *lc, *rc;
};
#define MID 81
int a[MID*2];
int pre_create_tree(int pos) {
int v;
scanf("%d", &v);
if( -1 == v ) return 1;
a[MID+pos] += v;
pre_create_tree(pos-1);
pre_create_tree(pos+1);
return 0;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int flag, i(1);
while(true) {
flag = 0;
memset(a, 0, sizeof(a));
if( pre_create_tree(0) ) break;
printf("Case %d:\n", i++);
for(int i = 0; i < MID*2; i ++) {
if( !a[i] ) continue;
if( !flag ) {printf("%d", a[i]); flag = 1;}
else printf(" %d", a[i]);
} www.zzzyk.com
printf("\n\n");
}
return 0;
}
补充:软件开发 , C++ ,