题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768
【分析】
思路:重写cmp函数,先财富值排序,然后年龄,最后姓名。定义普通数组的话会超时,所以用容器,把数据压入vector进行处理。
【代码】
#include<bits/stdc++.h>
using namespace std;
typedef struct node{char name[10];int age;int nw;
}node;
bool cmp(node a,node b)
{if(a.nw==b.nw){return a.age!=b.age?a.age<b.age:strcmp(a.name,b.name)<0;}return a.nw>b.nw;
}
int main()
{int n,k;scanf("%d%d",&n,&k);vector<node>v;for(int i=0;i<n;i++){node person;scanf("%s",person.name);scanf("%d%d",&person.age,&person.nw);v.push_back(person);}sort(v.begin(),v.end(),cmp);for(int ca=1;ca<=k;ca++){int x,y,z;scanf("%d%d%d",&x,&y,&z);printf("Case #%d:\n",ca);int cnt=0;int size=v.size();for(int i=0;i<size;i++){if(v[i].age>=y&&v[i].age<=z){printf("%s %d %d\n",v[i].name,v[i].age,v[i].nw);cnt++;}if(cnt==x)break;}if(!cnt)printf("None\n");}return 0;
}