PAT-B 1015. 德才论 (25)

来源:互联网 发布:白话翻译软件 编辑:程序博客网 时间:2024/05/01 23:08

题目链接在此。

题意理解

给出N个考生的准考证号、德分、才分,及格线L、有休闲H,然后对这个N个考生进行分类(这是这个题的关键):
1. 如果德分和才分有一个低于L,则为不及格考生,不参与最后的输出,为第五类
2. 如果德分和才分均不低于H,为第一类
3. 如果德分不低于H,才分低于H,为第二类
4. 如果德分和才分都低于H,但是德分不低于才分,为第三类
5. 其余为第四类

之后需要对这N个考生进行如下排序:
1. 先按照类别从小到大
2. 类别相同,按照总分从大到小
3. 总分相同,按照德分从大到小
4. 德分相同,按照准考证号从小到大

最后输出所有及格考生的相关信息,顺序为排序后的顺序。

思路

定义一个结构体数组,结构体成员见代码;
读取输入,将及格的考生信息放入结构体数组;
sort函数排序;
输出相关信息。

AC代码

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;struct INFO{    char id[9];    int cscore; //Charactoer 德分     int tscore; //Talented 才分     int sscore; //总分     int index; //考生类别 }stu[100005];int H;//比较函数bool cmp(INFO a, INFO b){    if(a.index != b.index) return a.index < b.index;    else if (a.sscore != b.sscore) return a.sscore > b.sscore;  // 总分排序     else if(a.cscore != b.cscore) return a.cscore > b.cscore; //总分相等,德分排序     else return (strcmp(a.id, b.id) < 0);  //德分相等,准考证号排序} int main(){//  int H;    int N, L, M = 0; //M为合格人数     scanf("%d %d %d",&N,&L,&H);    INFO temp;    while(N--){        getchar(); //吸收换行        scanf("%s %d %d",&temp.id,&temp.cscore,&temp.tscore);        temp.sscore = temp.cscore+temp.tscore;        //确定考生类别        if(temp.cscore>=H && temp.tscore>=H) temp.index = 1;        else if(temp.cscore>=H && temp.tscore<H) temp.index = 2;        else if(temp.cscore<H && temp.tscore<H && temp.cscore>=temp.tscore) temp.index =3;        else if(temp.cscore<L && temp.tscore<L) temp.index = 5;  //不合格考生,不参加排名         else temp.index = 4;        if(temp.cscore>=L && temp.tscore>=L){ //合格             stu[M++] = temp;         }    }    sort(stu,stu+M,cmp);    printf("%d\n",M);    for(int i = 0; i < M; i++){        printf("%s %d %d\n",stu[i].id, stu[i].cscore, stu[i].tscore);    }    return 0;}
0 0