题目
![]()
解法1
点击查看代码
#include <iostream>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
map<int, float, greater<int>> polyA; // 按指数降序排列
map<int, float, greater<int>> polyB; // 按指数降序排列
map<int, float, greater<int>> polyC; // 按指数降序排列
// 读取第一个多项式
int K, exp;
float cof;
cin >> K;
while (K--) {
cin >> exp >> cof;
polyA[exp] += cof;
}
// 读取第二个多项式
cin >> K;
while (K--) {
cin >> exp >> cof;
polyB[exp] += cof;
}
for(auto itA : polyA){
for(auto itB : polyB){
// 指数相加,系数相乘
int expC = (itA.first +itB.first);
float cofC = itA.second*itB.second;
polyC[expC] += cofC;
}
}
// 统计数量,过滤掉系数为 0 的项(系数正负抵消得0)
int count = 0;
for (auto it : polyC) {
if (it.second != 0.0f) count++;
}
cout<<count;
for(auto it : polyC){
if (it.second != 0.0f)
printf(" %d %.1f",it.first,it.second);
}
return 0;
}