PAT 1055. The World's Richest

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1055

题解:

枚举查找符合条件的输出即可。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x6fffffff
struct person
{
    char name[10];//姓名
    int age;//年龄
    int netWorth;//净资产
}richer[100005];
int q[100005];
int ageRank[205];
bool cmp(const struct person &a,const struct person &b)
{//按 净资产 年龄 姓名 排序
    if(a.netWorth!=b.netWorth)
        return a.netWorth>b.netWorth;
    else if(a.age!=b.age)
        return a.age<b.age;
    else
        return strcmp(a.name,b.name)<0;
}
int main()
{
    int n,m,k,aMin,aMax;
    int idx=0,num,age;
    memset(ageRank,0,sizeof(ageRank));
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;++i)
        scanf("%s%d%d",richer[i].name,&richer[i].age,&richer[i].netWorth);
    sort(richer,richer+n,cmp);
    for(int i=0;i<n;++i)
    {
        ++ageRank[richer[i].age];
        if(ageRank[richer[i].age]<101)//去除年龄大于100的
            q[idx++]=i;
    }
    for(int i=1;i<=k;++i)
    {
        scanf("%d%d%d",&m,&aMin,&aMax);
        printf("Case #%d:\n",i);
        num=0;
        for(int j=0;j<idx&&num<m;++j)
        {
            age=richer[q[j]].age;
            if(aMin<=age&&age<=aMax)
            {
                printf("%s %d %d\n",richer[q[j]].name,richer[q[j]].age,richer[q[j]].netWorth);
                ++num;
            }
        }
        if(num==0)
            printf("None\n");

    }
    return 0;
}

来源: http://blog.csdn.net/acm_ted/article/details/20659201


你可能感兴趣的:(PAT 1055. The World's Richest)