题解 | #合并表记录#

合并表记录

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

先读入键值对的个数n,再循环读入n个键值对,用map存储,如果能在map中找到数据则累加值

#include <iostream>
#include <map>

using namespace std;

int main()
{
    int n;
    int index,value;
    map<int,int> m_map;
    //读入键值对的个数;
    scanf("%d", &n);
    //循环读入;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d", &index, &value);
        map<int,int>::iterator iter = m_map.find(index);
        if(iter != m_map.end())
        {
            iter->second += value;
        }
        else
        {
            m_map[index] = value;
        }
    }
    //输出;
    for(auto& o: m_map)
    {
        printf("%d %d\n", o.first, o.second);
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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