7-31 The World's Richest (25分)

 

 

 

 

 

 解题思路:结构体排序

按题目要求排序,再按年龄段和最大输出数目输出

#include <stdio.h>
#include <string.h>
typedef char Element[9];
typedef struct {
    Element Name;
    int age,wealth;
} Bilionaires;
int cmp(const void *a,const void *b) {
    Bilionaires *c=(Bilionaires*)a;
    Bilionaires *d=(Bilionaires*)b;
    if(c->wealth==d->wealth) {
        if(c->age==d->age)
            return strcmp(c->Name,d->Name);
        return c->age-d->age;
    }
    return d->wealth-c->wealth;
}
int main() {
    int n,k,i;
    int cnt,x,y;
    scanf("%d%d",&n,&k);
    Bilionaires B[n];
    for(i=0; i<n; i++) {
        scanf("%s%d%d",B[i].Name,&B[i].age,&B[i].wealth);
    }
    qsort(B,n,sizeof(B[0]),cmp);
    for(i=0; i<k; i++) {
        scanf("%d%d%d",&cnt,&x,&y);
        printf("Case #%d:\n",i+1);
        int count=0;

        int j;
        for(j=0;j<n;j++) {
            if(B[j].age>=x&&B[j].age<=y) {
                printf("%s %d %d\n",B[j].Name,B[j].age,B[j].wealth);
                count++;
            }
            if(count==cnt)
                break;
        }
        if(!count)
            printf("None\n");

    }
    return 0;
}

 

posted @ 2020-05-01 23:15  跃鱼  阅读(194)  评论(0编辑  收藏  举报