码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-05-02 00:23:34      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:inf   typedef   return   none   char   info   code   div   main   

技术图片

 

 技术图片

 

 技术图片

 

 解题思路:结构体排序

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

#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分)

标签:inf   typedef   return   none   char   info   code   div   main   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!