PAT 甲级 1002 A+B for Polynomials
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000
题意
给出两个多项式,输出二者相加后非零项的总个数及其次方、系数。
代码
#include <bits/stdc++.h> using namespace std; int main() { map<int, double> poly; for (int i = 0; i < 2; i++) { int n; cin >> n; for (int j = 0; j < n; j++) { int x; double y; cin >> x >> y; poly[x] += y; } } vector<pair<int, double>> res; for (auto it = poly.rbegin(); it != poly.rend(); ++it) { if (it->second != 0.0) { res.emplace_back(it->first, it->second); } } cout << res.size(); for (auto pr : res) { cout << ' ' << pr.first << ' ' << setiosflags(ios::fixed) << setprecision(1) << pr.second; } }

浙公网安备 33010602011771号