首页 > 其他 > 详细

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

时间:2020-05-01 23:27:41      阅读:66      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 技术分享图片

 

 技术分享图片

 

 解题思路:结构体排序

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

#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;
}

技术分享图片

 

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

原文:https://www.cnblogs.com/snzhong/p/12815334.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!