PAT 1002

算法:multimap


注意:

①求和后系数为0时,不计算其项数。

②精确度。


#include <iostream>
#include <map>
#include <math.h>
using namespace std;

multimap<int, float> S1;
map<int, float> S2;
int g_K = 0;//S3的项数

void input()
{
	int K;
	cin >> K;
	while (--K >= 0)
	{
		int N;
		float A;
		cin >> N >> A;
		S1.insert(pair<int, float>(N, A));
	}

	cin >> K;
	while (--K >= 0)
	{
		int N;
		float A;
		cin >> N >> A;
		S1.insert(pair<int, float>(N, A));
	}
	return;
}

void func()
{
	for (auto c : S1)
	{
		if (S2.find(c.first) != S2.end())//已经计算过了
			continue;

		float sum = 0.0;
		for (auto beg = S1.lower_bound(c.first),
			end = S1.upper_bound(c.first);
			beg != end; ++beg)
		{
			sum += beg->second;
		}
		if (fabs(sum) < 0.00001)///和为0,则不计入项数
		{
		}
		else
		{
			S2.insert(pair<int, float>(c.first, sum));
			g_K++;
		}

	}
}

int main()
{
	input();
	func();
	cout << g_K;
	map<int, float>::reverse_iterator iter;
	for (iter = S2.rbegin(); iter != S2.rend(); iter++)
	{
		cout << ' ' << iter->first << ' ';
		printf("%.1f", iter->second);
	}
	cout << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值