PAT 乙级 1015 德才论

6 篇文章 0 订阅

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤10​5​​),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

 C语言实现

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
enum StuType {
    MOH_ABH = 0,    // 德才兼备
    MOH_ABL = 1,    // 德胜才
    MOL_ABL = 2,    // 德高于才
    ABL_MOL = 3,    // 才高于德
    LOSER = 4       // 不达标
};

struct StuInfo {
    int id;
    int mo_score;
    int ab_score;
    enum StuType type;
};
int cmp (void *p1, void *p2) {
    struct StuInfo *stu1 = (struct StuInfo *)p1;
    struct StuInfo *stu2 = (struct StuInfo *)p2;
    if ((int)(stu1->type) != (int)(stu2->type)) {
        return (int)(stu1->type) - (int)(stu2->type);
    } else if (stu1->mo_score + stu1->ab_score != stu2->mo_score + stu2->ab_score) {
        return stu2->mo_score + stu2->ab_score - stu1->mo_score - stu1->ab_score;
    } else if (stu1->mo_score != stu2->mo_score) {
        return stu2->mo_score - stu1->mo_score;
    } else {
        return stu1->id - stu2->id;
    }
}
      
int main() {
    int num, L, H;
    int cnt = 0;
    scanf("%d%d%d", &num, &L, &H);
    struct StuInfo *pStu = malloc(sizeof(struct StuInfo) * num);
    for (int i = 0; i < num; ++i) {
        scanf("%d%d%d", &pStu[i].id, &pStu[i].mo_score, &pStu[i].ab_score);
        if (pStu[i].mo_score >= H && pStu[i].ab_score >= H) {
            pStu[i].type = MOH_ABH;
            ++cnt;
        } else if (pStu[i].mo_score >= H && pStu[i].ab_score < H && pStu[i].ab_score >= L) {
            pStu[i].type = MOH_ABL;
            ++cnt;
        } else if (pStu[i].ab_score >= L && pStu[i].mo_score >= pStu[i].ab_score) {
            pStu[i].type = MOL_ABL;
            ++cnt;
        } else if (pStu[i].mo_score >= L && pStu[i].ab_score >= L) {
            pStu[i].type = ABL_MOL;
            ++cnt;
        } else {
            pStu[i].type = LOSER;
        }
    }
    qsort(pStu, num, sizeof(struct StuInfo), cmp);
    bool bStart = true;
    printf("%d\n", cnt);
    for (int i = 0; i < cnt; ++i) {
        printf("%d %d %d\n", pStu[i].id, pStu[i].mo_score, pStu[i].ab_score);
    }
    free(pStu);
    return 0;
}

1.过程比较繁琐,重点在于比较方法函数。将不同的情况用type表示可以简化排名比较。

2.先对类型作比较,同一类型的先以总分比较。总分相同,比较德分,德分相同比较学号。

3.申请的内存记得释放。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值