题解 | 合并表记录

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

#include <iostream>
#include <map>
#include <string>
#include <set>
using namespace std;

// class Po{
//     public:
//         int x;
//         int y;
//         Po(int a, int b) : x(a), y(b){};
//         friend bool operator< (const Po& p1, const Po& p2){
//             return p1.x < p2.x;
//         }
// };

int main() {
    int n;
    cin >> n;
    map<int, int> map;
    while (n--) {
        int a, b;
        cin >> a >> b;
        auto it = map.find(a);
        if(it != map.end()){
            map[a] += b;
        }else{
            map[a] = b;
        }
    }
    for(auto i : map){
        cout << i.first << " " << i.second << endl;
    }
    // set<Po> set;
    // for (int i = 0; i < n; i++){
    //     int a, b;
    //     cin >> a >> b;
    //     auto it = set.find(Po(a, b));
    //     if(it != set.end()){
    //         Po temp = *it;
    //         temp.y += b;
    //         set.erase(it);
    //         set.insert(temp);
    //     }else{
    //         set.insert(Po(a, b));
    //     }
    // }
    // for(auto it : set){
    //     cout << it.x << " " << it.y << endl;
    // }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务