0
点赞
收藏
分享

微信扫一扫

HJ8 合并表记录(c++)

青乌 2022-02-22 阅读 25

在这里插入图片描述

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

int main(){
    int n;
    cin >> n;
    map<int,int>m;
    for(int i = 0; i < n; i++){
        pair<int,int>item;
        cin >> item.first;
        cin >> item.second;
        if(m.find(item.first) != m.end()){//找到了相同索引
            m[item.first] += item.second;//合并后的键值对(多行
        }else{
            m[item.first] = item.second;
        }
    }
    for(auto it = m.begin(); it != m.end(); it++){
            cout << it->first << ' '<< it->second << endl;
        }
    return 0;
    
    
}
举报

相关推荐

0 条评论