题解 | #最小年龄的3个职工#定义个小根堆就行

最小年龄的3个职工

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

#include <iostream>
#include <queue>
using namespace std;
struct Staff {
    int id;
    string name;
    int age;
    bool operator<(const Staff& other) const {
        if (age != other.age) return age > other.age;
        else if (id != other.id) return id > other.id;
        return name > other.name;
    }
};
int main() {
    int n;
    cin >> n;
    priority_queue<Staff> pq;
    while (n--) {
        Staff staff;
        cin >> staff.id >> staff.name >> staff.age;
        pq.push(staff);
    }
    for (int i = 0; i < 3 && !pq.empty(); i++) {
        cout << pq.top().id << " " << pq.top().name << " " << pq.top().age << endl;
        pq.pop();
    }
    return 0;
}

全部评论

相关推荐

01-23 19:12
门头沟学院 Java
榨出爱国基因:你还差 0.1% 就拿到校招礼盒,快叫朋友给你砍一刀吧
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
2024-12-26 20:46
复旦大学 C++
国棉17厂丶小王:拿了offer的那个周末晚上去网吧通宵,去网吧不知道玩什么刷了lc的每日一题,然后试着第一次打开了三角洲行动,从此少了一个已经刷了700道题的lc用户,但是烽火地带多了一只🐭🐭
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务