题目:HJ8 合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
思路:存到map里,都存完遍历map
代码如下
#include <iostream>
#include <map>
using namespace std;
int main() {
int num = 0;
cin >> num;
if (num == 0) {
return 0;
}
int index = 0;
int value = 0;
map<int, int> tmp;
while (num--) {
cin >> index;
cin >> value;
map<int, int>::iterator it = tmp.find(index);
if (it == tmp.end()) {
tmp.insert(pair<int, int>(index, value));
} else {
int rowValue = it->second;
tmp.erase(it);
tmp.insert(pair<int, int>(index, value + rowValue));
}
}
for (auto it = tmp.begin(); it != tmp.end(); it++) {
cout << it->first << " " << it->second << endl;
}
return 0;
}
map::find
map::insert
谢谢观看,祝顺利!