输入描述:
先输入键值对的个数 然后输入成对的index和value值,以空格隔开
输出描述:
输出合并后的键值对(多行)
示例1
输入
4 0 1 0 2 1 2 3 4
输出
0 3 1 2 3 4
//C++
//C++,用stl中的map#include<iostream>#include<map>using
namespace
std;int
main(){ int
n; while(cin
>> n){ map<int,int>
m; while(n--){ int
key,value; cin
>> key >> value; if(!m[key]){ m[key]
= value; } else
m[key] += value;//不存在时赋值,存在时累加 } //map内部本身就是按照key的大小顺序进行存储的 for(map<int,int>::iterator
it=m.begin();it!=m.end();++it){ cout
<< it->first << "
"<<
it->second << endl; } } return
0;}

被折叠的 条评论
为什么被折叠?



