题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <iostream>
#include <map>
using namespace std;
/**
* 直接用 map 自带 index 有序和去重
*
* 自己实现 新建一个类或结构体成员 index value,放入到 vector 中;
* 在插入数据时,查找 index 是否存在,存在则相加
* 在 vector 中 按 index 排序。
*/
int main() {
map<int, int> map;
int index, value;
int n = 5;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> index >> value;
map[index] += value;
}
for (const auto& [key, value] : map) {
cout << key << " " << value << endl;
}
return 0;
}
查看2道真题和解析