题解 | #合并表记录#

合并表记录

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")

全部评论

相关推荐

06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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