题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000
题目大意
给定AB两个多项式(各一行)
2 1 2.4 0 3.2
2 2 1.5 1 0.5
格式如下
其中K是多项式中非零项的个数,N分别是指数和系数。
输出格式也按照上方。
解题思路
map暴力。
需要注意的地方是,当多项式系数为0时,指数和系数都不需要输出,相应的;当多项式整体为0时,只需要输出0。有亿点坑。
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,double> PID;
#define mp make_pair
int main(){
map<int,double> m;
int n;
cin >> n;
for(int i = 0;i < n;i++){
int x;
double y;
cin >> x >> y;
if(m.count(x) == -1){
m[x] = y;
}else{
m[x] += y;
}
}
cin >> n;
for(int i = 0;i < n;i++){
int x;
double y;
cin >> x >> y;
if(m.count(x) == -1){
m[x] = y;
}else{
m[x] += y;
}
}
stack<PID> st;
int all = 0;
for(auto it : m){
//cout << it.first << "->"<< it.second << "\n";
if(it.second != 0){
all++;
st.push(mp(it.first,it.second));
}
}
if(all == 0){
cout << all;
}else{
cout << all;
while(!st.empty()){
cout << " "<< st.top().first << " ";
printf("%.1f",st.top().second);
st.pop();
}
}
return 0;
}