题解 | #合并表记录#

合并表记录

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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool mycompare(pair<int,int>a,pair<int,int>b){
    return a.first < b.first;
}
int main() {
    int n,index,value;
    vector<pair<int,int>> v_table;
    cin >> n;
    for(int i = 0;i < n;i++){
        cin >> index >> value;
        v_table.push_back(make_pair(index,value));
    }
    sort(v_table.begin(),v_table.end(),mycompare);
    // vector<int> v_table1;
    for(int i = 0;i < v_table.size() - 1;i++){
        if(v_table[i].first == -1) continue;
        for(int j = i + 1;j < v_table.size();j++){
            if(v_table[i].first == v_table[j].first){
                v_table[i].second += v_table[j].second;
                v_table[j].first = -1;
            }
        }
        // v_table1.push_back(v_table[i].second);
    }
    for(auto v : v_table)
        if(v.first != -1){
            cout << v.first << " " <<v.second << endl;
        }

}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务