题解 | #合并表记录#

合并表记录

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

使用map记录val,利用vector记录索引并去重,排序后依次输出

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

int main() {
    int num = 0;
    cin >> num;

    std::map<int, int> count;
    std::vector<int> store_ind;

    for(int i = 0; i < num; i ++){
        int ind, val = 0;
        cin >> ind >> val;

        if(count.find(ind) == count.end()){
            count[ind] = val;
            store_ind.push_back(ind);
        }
        else
            count[ind] += val;
    }
    sort(store_ind.begin(), store_ind.end());
    for(int i = 0; i < size(store_ind); i ++){
        cout << store_ind[i] << " " << count[store_ind[i]] << endl;
    }

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

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
买蜜雪也用卷:我觉得应该没有哪个人敢说自己熟练使用git,代码分支一复杂还是得慢慢寻思一下的,不过基本的拉代码提交代码还有分支什么的是应该会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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