PAT (Advanced Level) 1009 Product of Polynomials

题意

多项式乘法。

思路

水~

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	map<int, double> a, b, mul;
	auto input = [](map<int, double> &mp) {
		int n;
		cin >> n;
		for (int i = 0, k; i < n; ++i) {
			double ak;
			cin >> k >> ak;
			mp[k] += ak;
		}
	};
	input(a);
	input(b);
	for (auto e1 : a)
		for (auto e2 : b)
			mul[e1.first + e2.first] += e1.second * e2.second;
	vector<pair<int, double>> ans;
	for (auto e : mul) {
		if (e.second == 0) continue;
		ans.push_back(e);
	}
	reverse(ans.begin(), ans.end());
	cout << ans.size();
	for (int i = 0; i < ans.size(); ++i) {
		cout << ' ' << ans[i].first << ' ';
		cout << fixed << setprecision(1) << ans[i].second;
	}
	return 0;
} 

HINT

不定时更新更多题解,详见 git ! ! !

发布了101 篇原创文章 · 获赞 16 · 访问量 4616

猜你喜欢

转载自blog.csdn.net/abcdefbrhdb/article/details/104660778