题解 | #最小年龄的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;
}

全部评论

相关推荐

怎么起名字:早知道就不读书了,害得我送外卖还得扶眼镜
点赞 评论 收藏
分享
zhch7:建议9✌️把学历加黑加粗,如果实在offer可能是觉得佬不会去
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务