#合并表记录#__huawei-no.8-1

合并表记录

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

#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> arr;
    unordered_map<int,int>map1;
    while(n-- >0){
        int key,val;
        cin >> key >> val;
        if(map1.count(key) == 0){
            map1[key] = val;
            arr.push_back(key);
        }
        else {
            map1[key] += val;
        }
    }
    sort(arr.begin(),arr.end());
    for(int & i : arr){
        cout << i <<" "<< map1[i] <<endl;
    }
}

// 64 位输出请用 printf("%lld")

非常好的题目,增进我对哈希表的理解,主要是这个count函数的使用,真的不错。

全部评论

相关推荐

08-21 16:35
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务