题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
使用map记录val,利用vector记录索引并去重,排序后依次输出
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; int main() { int num = 0; cin >> num; std::map<int, int> count; std::vector<int> store_ind; for(int i = 0; i < num; i ++){ int ind, val = 0; cin >> ind >> val; if(count.find(ind) == count.end()){ count[ind] = val; store_ind.push_back(ind); } else count[ind] += val; } sort(store_ind.begin(), store_ind.end()); for(int i = 0; i < size(store_ind); i ++){ cout << store_ind[i] << " " << count[store_ind[i]] << endl; } } // 64 位输出请用 printf("%lld")