map 题解 | #求两个多项式的和#
求两个多项式的和
https://www.nowcoder.com/practice/a9901ba9163549a590d1d1c245f14d2a
#include <iostream> #include <map> using namespace std; map<int, int, greater<int>> mp; //次数, 系数 int main(){ int n, m; cin>>n; for(int i = 0; i < n; i ++){ int a, b; cin>>a>>b; mp[b] = a; } cin>>m; for(int i = 0; i < m; i ++){ int a, b; cin>>a>>b; if(mp.count(b) == 0) mp[b] = a; else mp[b] += a; } for(auto i : mp){ if(i.second != 0) cout<<i.second<<' '<<i.first<<' '; } }