题解 | #合并表记录#

合并表记录

http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

#include <iostream>
#include<map>

struct CmpByKeyLength {
  bool operator()(const int& k1, const int& k2) {
    return k1 < k2;
  }
};

using namespace std;

int main()
{
    int n;
    cin>>n;
    map<int, int, CmpByKeyLength> mymap;
    int a,b;
    for(int i=0;i<n;++i){
        cin>>a>>b;
        if(mymap.find(a) == mymap.end()){
            mymap[a] = b;
        }else
            mymap[a]+=b;
    }
    for(auto it=mymap.begin();it!=mymap.end();++it){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务