题解 | #合并表记录#

合并表记录

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")

全部评论

相关推荐

球Offer上岸👑:可能是大环境太差了 太卷了 学历也很重要 hc也不是很多 所以很难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务