HJ8 合并表记录

作为只熟悉语言基础的人来说,这样的题目绝对眼前一黑。
找了找文章
http://t.csdnimg.cn/guiOu
去学习了map<int,int>::iterator it ,主要来自以下:
http://t.csdnimg.cn/9HGPK
http://t.csdnimg.cn/bLtOl
差不多搞懂了,就开始写了。因为是第一次,所以加一点注释。

#include <iostream>
using namespace std;

#include<map>
int main() {
    int num;
    cin >> num;//输入组数,但是这里没必要用到
    int index,value;//设定index和value,index作为map编号,value是内容
    map<int,int>MAP;//生成一个map,下面开始输入
    while(cin >> index && cin >> value)
    {
        MAP[index]=MAP[index]+value;
    }
    map<int, int>::iterator it;//开始迭代,输出数据
    for( it = MAP.begin(); it != MAP.end(); it ++)
    {
        cout <<it->first<<' '<<it->second<<endl;//第一次看到,留意一下
    }
}
// 64 位输出请用 printf("%lld")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值