题解 | #合并表记录#

合并表记录

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

好痛苦,好久不写了,手都生了
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

void print( map<int, int> mp);
int main() {
   
    map<int, int> ans;
    int n; cin>>n;

    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        if(ans.find(a) == ans.end()){   //没找到,直接插入
            ans[a] = b;
        }else{
            ans[a] += b;
        }       
    }

    print(ans);
}
void print( map<int, int> mp){
    for(auto it : mp){
        cout<<it.first<<" "<<it.second<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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