uva 699 the falling leaves——yhx

因为复制过来排版很乱,所以上截图。

 1 #include<cstdio>
 2 #include<cstring>
 3 int sum[10010];
 4 void bd(int p)
 5 {
 6     int x;
 7     scanf("%d",&x);
 8     if (x==-1) return;
 9     sum[p]+=x;
10     bd(p-1);
11     bd(p+1);
12 }
13 bool dl()
14 {
15     int i,j,k,p,q,x,y,z;
16     memset(sum,0,sizeof(sum));
17     scanf("%d",&x);
18     if (x==-1) return 0;
19     sum[5000]=x;    //树根建在最中间。
20     bd(4999);
21     bd(5001);
22 }
23 int main()
24 {
25     freopen("in.txt","r",stdin);
26     int n,i,j,k=0;
27     while (dl())
28     {
29         i=0;
30         while (!sum[i]) i++;  //找见最左端的结点
31         printf("Case %d:\n%d",++k,sum[i]);
32         while (sum[++i]) printf(" %d",sum[i]);
33         printf("\n\n");  //注意回车
34     }
35 }

利用先序的特性可以递归处理。

时间: 2024-07-29 14:20:21

uva 699 the falling leaves——yhx的相关文章

[2016-02-09][UVA][699][The Falling Leaves]

时间:2016-02-09 13:29:10 星期二 题目编号:UVA 699 题目大意:  给一棵树,每棵树有一个叶子,叶子的值是点权,求叶子垂直落下后, (同一个方向的形成一堆),求每堆叶子的总权值 位置的描述:每个左子树在根左边一个单位,右子树在根右边一个单位 分析: 遍历一遍二叉树,传参保存每个二叉树的位置,最后保存即可 每行不超过80个字符,那么节点数不大于80个,堆数不大于80个 方法:dfs,递归 解题过程遇到问题: 一行数据不一定就是一颗完整的树!!!!!!! 开始还以为读取到第

UVa 699.The Falling Leaves【7月23】

The Falling Leaves Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how lar

UVA 699 The Falling Leaves

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<map> #include<algorithm> #include<cctype>

UVA 699 The Falling Leaves (二叉树水题)

本文纯属原创,转载请注明出处,谢谢.http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the sam

UVa 699 The Falling Leaves(递归建树)

题意  假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和上一题有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #include<cstdio> #include<cstring> using namespace s

uva 699 The Falling Leaves(建二叉树同一时候求和)

本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开一个数组.这里我是把根节点作为第42个数,能够在建树的同一时候求 出那一列全部数值的和左孩子节点减一,右孩子节点加一.. .写的时候中间出了点小bug,忘了给flag重置0了,调 了好久.. . 第一次提交wa了,由于没有换行,题目要求结果之间有一行空行的. . . 贴代码: #include<st

UVA 399 The Falling Leaves(二叉树)

 The Falling Leaves  Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how l

UVA 699(二叉树建树与遍历)

M - The Falling Leaves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-02-08) Description  The Falling Leaves  Each year, fall in the North Central region is accompanied

【数据结构】The Falling Leaves(6-10)

[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1,向右+1,统计答案即可. #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #in