题解 | #最小年龄的3个职工#
最小年龄的3个职工
https://www.nowcoder.com/practice/a9fa482eb05149cdbad88689e3cb5f66
#include<iostream> #include<algorithm> #include<string.h> using namespace std; struct worker{ int id; char name[11]; int age; }; bool comp(worker lhs,worker rhs){ if(lhs.age<rhs.age){ return true; } else if(lhs.age==rhs.age&&lhs.id<rhs.id){ return true; } else if(lhs.age==rhs.age&&lhs.id==rhs.id&&strcmp(lhs.name,rhs.name)<0){ return true; } else return false; } int main() { int n; worker w[1000]; while(scanf("%d",&n)!=EOF){ int i; for(i=0;i<n;i++){ scanf("%d%s%d",&w[i].id,w[i].name,&w[i].age); } sort(w,w+n,comp); for(i=0;i<3;i++){ printf("%d %s %d\n",w[i].id,w[i].name,w[i].age); } } }