数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。
#include<iostream>
#include<map>
using namespace std;
int
main(){
int
n;
map<
int
,
int
> m;
cin>>n;
while
(n--){
int
key,value;
cin>>key>>value;
if
(!m[key])
m[key]=value;
else
m[key]+=value;
}
for
(map<
int
,
int
>::iterator it=m.begin();it!=m.end();++it)
cout<<it->first<<
' '
<<it->second<<endl;
return
0
;
}