PAT-A1012 The Best Rank 题目内容及题解

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 C, M, E 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 C, M 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

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

题目大意

题目给出一个成绩单和一系列学生ID请求,需要返回其表现最优秀的科目排名;如果学生不存在,则返回“N/A”;

解题思路

本道题排序项目较多且需要返回名次,但是数据范围有限,因此采用了空间换时间的策略以节约运行时间。

  1. 初始化人数分段和排名的二维数组;
  2. 依次读入学生ID、三科成绩并计算均分,保存分数进学生的结构体中,并进行计数;
  3. 读取完毕后处理分数段计数,从而获取对应分数的排名;
  4. 依次读取请求并输出;
  5. 输出结束后返回0值。

代码

#include<stdio.h>

struct STU{
    int ID;
    int grade[4];//A,C,M,E;
    int vivid;
}stu[1000010];

char CH[6]="ACME";

int rank[4][110];//排名 
int num[4][110];//人数分段 ACME

void Init(){
    int i,j;
    for(i=0;i<4;i++){
        for(j=0;j<110;j++){
            rank[i][j]=0;
            num[i][j]=0;
        }
    }
    return;
}

void GetGrade(int n){
    int i,temp,A,C,M,E;
    for(i=0;i<n;i++){
        scanf("%d",&temp);
        stu[temp].vivid=1;//学号有效
        scanf("%d%d%d",&C,&M,&E);
        num[1][C]++;
        num[2][M]++;
        num[3][E]++;
        A=( C + M + E +1)/3;
        num[0][A]++;
        stu[temp].grade[0]=A;
        stu[temp].grade[1]=C;
        stu[temp].grade[2]=M;
        stu[temp].grade[3]=E;
    }
    return;
}

void DataOp(){
    int i,j;
    for(i=0;i<4;i++){
        for(j=100;j>=0;j--){
            num[i][j]+=num[i][j+1];
            rank[i][j]=num[i][j+1]+1;
        }
    }
    return;
}

void print(int ID){
    int i,imax,grade,mgrade;
    if(stu[ID].vivid==0){
        printf("N/A\n");
    }else{
        mgrade=stu[ID].grade[0];
        imax=0;
        for(i=1;i<4;i++){
            if(rank[i][stu[ID].grade[i]]<rank[imax][mgrade]){
                imax=i;
                mgrade=stu[ID].grade[i];
            }
        }
        printf("%d %c\n",rank[imax][mgrade],CH[imax]);
    }
    return; 
}

int main(){
    int N,M,id;
    int i;
    scanf("%d%d",&N,&M);
    Init();
    GetGrade(N);
    DataOp();
    for(i=0;i<M;i++){
         scanf("%d",&id);
         print(id);
    }
    return 0;
}

运行结果

PAT-A1012 The Best Rank 题目内容及题解