题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201?tpId=37&tqId=21231&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

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

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        map<int, int> arr;	//有序
        for (int i = 0; i < n; i++) {
            int k, v;
            cin>>k>>v;
            if(!arr.count(k)){	//用count则只需一次查找即可判断是否存在某个键
                arr[k] = v;
            }else {
                arr[k] = v + arr[k];
            }
        }
        for (auto it : arr) {	//遍历arr中的键对
            cout<< it.first <<' ' <<it.second<<endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

就用这个吧:支持多益再加一个空气使用费
点赞 评论 收藏
分享
02-16 22:13
门头沟学院 Java
Yki_:女生学成这样挺不错了,现在停止网课,立刻all in八股,从最频繁的开始背,遇到不会的知识点直接问AI,项目也别手敲,直接看技术文档,背别人总结好的面试官可能问的问题的答案,遇到不会的再去代码里找具体实现就可以了,3月份开始边背边投实习约面
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务