题解 | #最小年龄的3个职工#

最小年龄的3个职工

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

#include<iostream>
#include<queue>
using namespace std;

struct worker{
    int id;
    string name;
    int age;
    worker(int i,string s,int a):id(i),name(s),age(a){}
    bool operator<(const worker& w)const{ //重载运算符实现最小堆,且按照年龄>工号>姓名优先级排序
        if(age==w.age && id==w.id){
            return name>w.name;
        }
        else if(age==w.age){
            return id>w.id;
        }
        return age>w.age;
    }
};
int main(){
    int n;
    while(cin>>n){
        priority_queue<worker> pq;
        int i,id,age;
        string str;
        for(i=0;i<n;i++){
            cin>>id>>str>>age;
            pq.push(worker(id,str,age));
        }
        for(i=0;i<3;i++){
            cout<<pq.top().id<<" "<<pq.top().name<<" "<<pq.top().age<<endl;
            pq.pop();
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务