1009 Product of Polynomials (25 分)

本文介绍了一段使用C++编程语言实现的代码,该代码利用标准模板库(STL)中的map容器进行数据处理。具体地,代码通过两个map容器a和b,分别读取并累加了输入的整数及其对应的双精度浮点数,然后通过遍历这两个map的迭代器,将每个元素相乘并累加到另一个map容器ans中。最终,程序输出了ans容器中元素的数量及各元素的键值对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
#include<iomanip>
using namespace std;

const int N = 1000+5;

map<int, double>a, b, ans;

int main() {

	int n, e;
	double c;

	cin >> n;

	for(int i=0; i<n; i++) {
		cin >> e >> c;
		a[e] += c;
	}
	cin >> n;

	for(int i=0; i<n; i++) {
		cin >> e >> c;
		b[e] += c;
	}
	int e1, e2;
	double c1, c2;

	for(map<int, double>::iterator it1=a.begin(); it1!=a.end(); it1++) {
		e1 = it1->first;
		c1 = it1->second;
		if(c1 == 0) continue;
		for(map<int, double>::iterator it2=b.begin(); it2!=b.end(); it2++) {
			e2 = it2->first;
			c2 = it2->second;

			if(c2 == 0) continue;

			ans[e1+e2] += c1*c2;

//			cout<< e1 << ":" << c1 << " " << e2 << c2 << " "<< e1+e2 << " " << ans[e1+e2] << endl;
		}
	}
	int cnt=0;
	for(int i=0; i<=2000; ++i) {
		if(ans[i])
			cnt++;
	}
	cout<<cnt;
	for(int i=2000; i>=0; --i)
		if(ans[i])
			cout<<" "<<i<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<ans[i];
	return 0;
}

//20分

#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>

using namespace std;

const int N = 1000+5;

map<int, double>a, b, ans;

int main() {

	int n, e;
	double c;

	cin >> n;

	for(int i=0; i<n; i++) {
		cin >> e >> c;
		a[e] += c;
	}
	cin >> n;

	for(int i=0; i<n; i++) {
		cin >> e >> c;
		b[e] += c;
	}
	int e1, e2;
	double c1, c2;

	for(map<int, double>::iterator it1=a.begin(); it1!=a.end(); it1++) {
		e1 = it1->first;
		c1 = it1->second;
		if(c1 == 0) continue;
		for(map<int, double>::iterator it2=b.begin(); it2!=b.end(); it2++) {
			e2 = it2->first;
			c2 = it2->second;

			if(c2 == 0) continue;

			ans[e1+e2] += c1*c2;

//			cout<< e1 << ":" << c1 << " " << e2 << c2 << " "<< e1+e2 << " " << ans[e1+e2] << endl;
		}
	}
	
	if(ans.size() == 0) {
		cout << 0;
	} else {
		map<int, double>::iterator it=ans.end();
		it--;

		cout << ans.size();

		for( ; it!= ans.begin(); it--) {
			e2 = it->first;
			c2 = it->second;

			printf(" %d %.1f", e2, c2);
		}
		e2 = it->first;
		c2 = it->second;
		printf(" %d %.1f", e2, c2);
	}

	return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值