题目链接:https://www.patest.cn/contests/pat-a-practise/1002
#include <iostream> #include <algorithm> #include <set> #include <map> #include <vector> #include <stack> #include <queue> #include <cmath> using namespace std; int main(int argc, char** argv) { int k,n; map<int,double> MM; stack<map<int,double>::iterator> SS; //记录迭代器 反向输出 for(int j=0;j<2;++j) { cin >> k; for(int i=0;i<k;++i) { int x; double y; cin >> x >> y; if(MM.count(x) == 0) MM[x] = y; else MM[x] += y; } } int cnt = 0; for(map<int,double>::iterator it = MM.begin();it != MM.end();++it) { if(abs(it->second) != 0.0) { ++cnt; SS.push(it); } } cout << cnt; while(!SS.empty()) { printf(" %d %.1f",SS.top()->first,SS.top()->second); SS.pop(); } cout<<endl; return 0; }