题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <iostream>
#include <unordered_map>
using namespace std;
#include <vector>
#include <algorithm>
struct PayRoll2{
int key;
int value;
};
int main() {
int n;
cin>>n;
unordered_map<int, int> hashtable;
vector<PayRoll2> vec;
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
if(hashtable.count(a) == 0){
hashtable[a] = b;
}else{
hashtable[a] += b;
}
}
for(auto& [key, value]: hashtable){
// cout<<key<<" "<<value<<endl;
vec.push_back(PayRoll2{key, value});
}
sort(vec.begin(), vec.end(), [](PayRoll2 a, PayRoll2 b){
return a.key < b.key;
});
for(auto & tmp:vec){
cout<< tmp.key<<" "<<tmp.value<<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
查看24道真题和解析
阿里云成长空间 794人发布