题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
/*struct cmp{
bool operator()(pair<int, int>& m, pair<int, int>& n) {
return m.first < n.first;
}
};*/
int main(){
int num = 0;
cin >> num;
map<int, int> m; //map内部本身就是按照key的大小顺序进行存储的
//vector<pair<int, int>> vec;
for(int i = 0; i < num; i++){
int key = 0, value = 0;
while(cin>>key>>value){
if(!m[key]){
m[key] = value;
}
else m[key] += value;//不存在时赋值,存在时累加
//vec.push_back(make_pair(key, value));
}
}
//sort(vec.begin(), vec.end(), cmp());
//map内部本身就是按照key的大小顺序进行存储的
for(map<int, int>::iterator/*auto*/ iter = m.begin(); iter != m.end(); iter++){
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
华为题库题解 文章被收录于专栏
牛客华为题库的题解