uva 699

原题

简单题, 又是构造树而且不用存储的, 这种都不用实际地去构造一种struct,

直接递归然后计算即可, 

这里像是一个边界不固定的数组, 下标有负数

 所以我的思路是用一个map来记录每个pos的数量, 代码还是比较清晰, 就不多说了


话说用好递归真是事半功倍

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
const int MAXN = 10005;
typedef long long LL;
/*
uva 699

*/

map<int,LL> Sum;

void drop(int pos){
	int n;
	cin >> n;
//	cout << "pos=" << pos << ", n=" << n << endl;
	if( n!=-1 ){
		Sum[pos] += n; 
		drop(pos-1);
		drop(pos+1);
	}
}

int main(){
//	freopen("input2.txt","r",stdin);
	int n, Case = 0;
	while( scanf("%d",&n)!=EOF && n!=-1 ){
		Sum.clear();
		Sum[0]+=n;
		drop(-1);
		drop(1);
		map<int,LL>::iterator it = Sum.begin();
		cout << "Case " << (++Case) << ":" << endl;
		cout << it->second;
		it++; 
		while( it!=Sum.end() ){
			cout << " " << it->second;
			it++;
		}
		cout << endl << endl;
	}
	
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值