题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <iostream> using namespace std; #include<map> int main() { int n; cin >> n; map<int, int> m; int index, value; for (int i = 0; i < n; i++) { cin>>index>>value; if (m.count(index)) { m[index] += value; //index->value更新 } else { m.insert(pair<int, int>(index, value)); } } for (auto it = m.begin(); it != m.end(); it++) { cout << it->first << ' ' << it->second << endl; } return 0; } // int main() { // int n; // cin>>n; // map<int,int> m; // string s(3, 'a'); // while (getline(cin, s)) { // 注意 while 处理多个 case // int index = s[0], value = s[2]; // if(m.count(index)) // { // m[index] += value; //index->value更新 // } // else { // m.insert(pair<int,int>(index,value)); // } // } // for(auto it = m.begin(); it != m.end(); it++) // { // cout<<it->first<<' '<<it->second<<endl; // } // return 0; // } // 64 位输出请用 printf("%lld")