作为只熟悉语言基础的人来说,这样的题目绝对眼前一黑。
找了找文章
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")