题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <iostream> #include <sstream> #include <unordered_map> #include <map> using namespace std; int main() { map<int, int> res; int num; cin>>num; int index; int val; string s; for(int i = 0;i<num+1;i++){ getline(cin,s); istringstream ss(s); ss>>index; ss>>val; if(s.size()==0){ continue; } if(res.find(index)==res.end()){ res.insert({index,val}); }else{ res[index] += val; } } for(auto x:res){ cout<<x.first<<' '<<x.second<<endl; } } // 64 位输出请用 printf("%lld")