题目
主体
This time, you are supposed to find A+B where A and B are two polynomials.
输入描述
输出描述
输入示例
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出示例
3 2 1.5 1 2.9 0 3.2
实现
思路
map数据结构,使用键值对
C++
#include <iostream>
#include <iomanip>
#include <map>
using namespace std;
int main(){
map<int, float> coefMap;
for (int i=0; i<2; i++){
int k;
cin >> k;
for (int j=0; j<k; j++){
int exp;
float coef;
cin >> exp >> coef;
coefMap[exp] += coef;
if (coefMap[exp]==0){
coefMap.erase(exp);
}
}
}
cout << coefMap.size();
for (auto it=coefMap.rbegin(); it!=coefMap.rend(); ++it){
cout << " " << it->first << " " << fixed << setprecision(1) << it->second;
}
cout << endl;
return 0;
}