题解 | #合并表记录#

合并表记录

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

//这题主要用到map的value和key的映射关系,先建立一个set利用set的有序性和不重复性建立key的索引,然后根据key值查询map中保存的value
#include <algorithm>
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main() {
    int a,k,v;
    cin>>a;
    set<int> key;
    map<int,int> m1;
    for(int i=0;i<a;i++)
    {
        cin>>k>>v;
        key.insert(k);
        if(find(key.begin(),key.end(),k)!=key.end())
        {
            m1[k]+=v;
        }
        else
        {
            m1[k]=0;
            m1[k]+=v;
        }

    }
    for(set<int>::iterator it=key.begin();it!=key.end();it++)
    {
        cout<<*it<<' '<<m1[*it]<<endl;
    }


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

全部评论
呼呼
点赞 回复 分享
发布于 2023-11-03 20:58 江西

相关推荐

评论
1
收藏
分享

创作者周榜

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