PAT 1012

1012 The Best Rank (25分)


To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of CME and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of CM and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

一、 试题分析:

  • 先输入N:考生数、M:查询成绩考生数(<=2000)
  • N行考生成绩登记:
  1. 每行第一列:考生的编号(六位)
  2. 三个科目C、M、E的分数( [0, 100])
  • M行:每行代表查询成绩的考生编号
  • 对M个查成绩的考生进行分析并输出:
  1. 该考生4个成绩(C、M、E、A-平均成绩)中,排名最高的名次以及对应的C、M、E、A中哪一个
  2. 若存在相同排名:按照优先级A>C>M>E输出对应的symbol
  3. 若该考生编号不在已登记成绩的考生中:输出N/A

二、解题思路:

  • 如何存储学生的编号和各科成绩?
  1. 将学生看成一个struct结构体stu[2001]:包括编号id和四个成绩score[4](便于编程思考,如果单单使用数组会显得结构性不够强)
  2. 平均分A存储进stu[i].score[3]:平均分的计算最好使用round函数对double类型数据四舍五入(但本题中直接做除法/也可以AC)
  • 如何得到每个学生各科成绩的排名?
  1. 由于要找到每个编号学生四个成绩的总排名:声明一个Rank[1000000][4]的数组(方便起见)
  2. 对每列成绩排序:直接使用sort(序列首地址,序列末地址,[cmp函数降序声明](省略则默认非降序))函数
  3. 名次正常时即为已按降序排好序列的下标值;分数相同时名次取前一个人的名次即可
  • 如何得到查询成绩学生的最高排名及对应科目名?
  1. 由于优先级是A>C>M>E:所以按照顺序分配序号course[4]数组的0123元素为ACME
  2. 首先读入查询ID,看其是否存在:不存在必定=0(若存在则一定从1开始计过数
  3. 若存在:由于优先级已通过数组限定了,这里直接选择较小者即可
  • ​​​​​​​注意:在c++中全局变量有初始默认值,但局部变量是随机分配的,若有定义一定要赋初值(如{0})
#include <iostream>
#include <algorithm>
using namespace std;
struct student{
    int id;
    int score[4];//每个人每门课的分数,每门课的排名
}stu[2010];

int Rank[1000000][4]={0};//初始化排名为0
int course;
char k[4]={'A','C','M','E'};

bool cmp(student a,student b){ //按照分数降序排序
    return a.score[course]>b.score[course]; //course为全局变量在主函数的循环中已赋值
}

int main(){
    int n,m;
    int query;
    cin>>n>>m;

    for(int i=0;i<n;i++){ //读入每个学生的id和三个分数
        cin>>stu[i].id>>stu[i].score[1]>>stu[i].score[2]>>stu[i].score[3];
        stu[i].score[0]=(stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3;//0A,1C,2M,3E
    }

    for(course=0;course<4;course++){ //对四个成绩分别排序
        sort(stu,stu+n,cmp); //sort函数的三个参数:所排序列的首地址、末地址、cmp(省略则为非降序)
        Rank[stu[0].id][course]=1; //排序完毕将分数最高的设置为rank1
        for(int i=1;i<n;i++){ //剩下的学生
            //如果和上一个学生分数相同
            if(stu[i].score[course]==stu[i-1].score[course]){
                Rank[stu[i].id][course]= Rank[stu[i-1].id][course]; //则排名相同
            }
            else
                Rank[stu[i].id][course]=i+1; //否则设置为本身正常的排序(即sort后的位置)
        }
    }

    for(int i=0;i<m;i++){
        cin>>query;
        if(Rank[query][0]==0)
            cout<<"N/A"<<endl;
        else{
            int temp=0;
            for(int i=0;i<4;i++){
                if(Rank[query][i]<Rank[query][temp])
                    temp=i;
            //选出被查询学生4科中排名最前的一科
            }
            cout<<Rank[query][temp]<<' '<<k[temp]<<endl;
        }
    }
    return 0;
}
发布了28 篇原创文章 · 获赞 2 · 访问量 6559

猜你喜欢

转载自blog.csdn.net/Ariel_x/article/details/104057417