UVA 699 The Falling Leaves 数据结构

本文解析了一道UVA平台上的题目,该题要求通过递归方式输入叶子节点权值,并输出每一列叶子节点的权值之和。文章提供了完整的C++代码实现,展示了如何通过递归构建树形结构并计算各列叶子节点的权值总和。

  题目链接: UVA

  题目描述: 递归方式输入叶子节点权值, 输出每一列的叶子节点的权值之和

  解题思路: 还是递归输入和上题差不多。

  代码: 

#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
#include <forward_list>
#include <sstream>
using namespace std;

const int maxn = 1e3+10;
int sum[maxn];

void build(int p) {
    int v;
    cin >> v;
    if(v == -1) return;
    sum[p] += v;
    build(p-1);
    build(p+1);
}
bool init() {
    int v;
    if((cin >> v)==EOF) return false;
    int pos = maxn >> 1;
    sum[pos] = v;
    build(pos-1);
    build(pos+1);    
    return true;
}
int main() {
    freopen("in.txt", "r", stdin);
    while(init()) {
        int p = 0;
        while(sum[p]==0) p++;
        while(sum[p]!=0) {
            cout << sum[p++] << " ";
        }
        cout << endl;
    }    
    return 0;
}
View Code

  思考: 很烦, 这道题写很烦, 这样吧, 一会儿再写写面试题

转载于:https://www.cnblogs.com/FriskyPuppy/p/8027633.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值