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

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

//职工信息
struct worker{
	int num;
	string name;
	int age;
};

//运算符重载
bool operator<(const worker& worker1,const worker& worker2){
	if(worker1.age==worker2.age){
		if(worker1.num==worker2.num){
			return worker1.name<worker2.name;
		}else{
			return worker1.num<worker2.num;
		}
	}else{
		return worker1.age<worker2.age;
	}
}

/*
 *最小年龄的三个员工
 * */
int main(){
	int n;
	while(cin>>n){
		worker* works = new worker[n];
		for(int i =0;i<n;i++){
			cin>>works[i].num;
			cin>>works[i].name;
			cin>>works[i].age;
		}
		sort(works,works+n);
		//输出最小的三个员工
		for(int i = 0;i<3;i++){
			cout<<works[i].num<<" "<<works[i].name<<" "<<works[i].age<<endl;
		}
	}
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务