标签:name nbsp turn 代码 慢慢 pre stdio.h include 努力
本来看着挺难的。大概是由于我多瞟了一眼题解,瞬间认为简单多了。做题就得这样,多自己想想。如今是
多校联赛,然而我并不会做。
。。
。慢慢来,一直在努力。
分析:
题上说了做多不会超过80行。所以能够开一个数组。这里我是把根节点作为第42个数,能够在建树的同一时候求
出那一列全部数值的和左孩子节点减一,右孩子节点加一。。
。写的时候中间出了点小bug,忘了给flag重置0了,调
了好久。。
。
第一次提交wa了,由于没有换行,题目要求结果之间有一行空行的。
。
。
贴代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
int cnt[100];
int flag,m;
int min,max;
typedef struct Tnode
{
int data;
struct Tnode *lchild;
struct Tnode *rchild;
}*node;
void creattree(node &T,int n)
{
int x;
if(flag)
{
x = m;
flag=0;
}
else
scanf("%d",&x);
if(x == -1)
{
T = NULL;
return ;
}
else
{
cnt[n] += x;
T = (node)malloc(sizeof(Tnode));
T->lchild = NULL;
T->rchild = NULL;
T->data = x;
}
creattree(T->lchild, n-1);
creattree(T->rchild, n+1);
if(n < min)
min = n;
if(n > max)
max = n;
return ;
}
int main()
{
int i;
int ans = 0;
while(scanf("%d",&m)&&m!=-1)
{
ans++;
min = INT_MAX;
max = 0;
flag = 1;
memset(cnt, 0, sizeof(cnt));
node T;
creattree(T,42);
printf("Case %d:\n",ans);
for(i=min; i<=max; i++)
{
printf("%d",cnt[i]);
if(i!=max)
printf(" ");
}
puts("");
puts("");
//flag = 0;
}
return 0;
}
uva 699 The Falling Leaves(建二叉树同一时候求和)
标签:name nbsp turn 代码 慢慢 pre stdio.h include 努力
原文地址:http://www.cnblogs.com/mthoutai/p/6730616.html