PAT_A 1012 The Best Rank

PAT_A 1012 The Best Rank

分析

题目要求将各学生的成绩进行排序,并输出查询的学号最靠前的排名和对应科目。

科目包括C语言(C)、数学(M)、英语(E)、平均分(A),这里的平均分为计算方便用总分替代。最后要求根据输入的ID查询对应学生最靠前的排名,及其对应的科目,若该ID不存在则输出N/A。值得注意的是排名会有并列的情况,如1、1、1、1、5。因此对输入的数据进行四次排名,每次均更新相应的位次信息。

由于最开始没有完全理解题意,导致出现了许多重复的代码片段,应引以为戒。

题目的描述

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

AC的代码

#include<bits/stdc++.h>

using namespace std;

class student{
    public:
    int ID,C,M,E,A,br;
    char subject;
    student(){}
    student(int id,int c,int m,int e){
        ID = id;
        C = c;
        M = m;
        E = e;
        A = c+m+e;
        br = 2001;
    }
};

void my_sorts(array<student,2001> &stu, int start, int end,char subject){
    if(end-start<2||start<0||end>2000)return;
    student t = stu[start];
    int i = start,j = end-1;
    switch (subject){
        case 'A':
            while(j>i){
                while(j>i&&t.A>stu[j].A)j--;
                if(j>i)stu[i++]=stu[j];
                while(j>i&&t.A<stu[i].A)i++;
                if(j>i)stu[j--]=stu[i];
            }
            break;
        case 'C':
            while(j>i){
                while(j>i&&t.C>stu[j].C)j--;
                if(j>i)stu[i++]=stu[j];
                while(j>i&&t.C<stu[i].C)i++;
                if(j>i)stu[j--]=stu[i];
            }
            break;
        case 'M':
            while(j>i){
                while(j>i&&t.M>stu[j].M)j--;
                if(j>i)stu[i++]=stu[j];
                while(j>i&&t.M<stu[i].M)i++;
                if(j>i)stu[j--]=stu[i];
            }
            break;
        case 'E':
            while(j>i){
                while(j>i&&t.E>stu[j].E)j--;
                if(j>i)stu[i++]=stu[j];
                while(j>i&&t.E<stu[i].E)i++;
                if(j>i)stu[j--]=stu[i];
            }
            break;
    }
    stu[i]=t;
    my_sorts(stu,start,i,subject);
    my_sorts(stu,i+1,end,subject);
}

void use_my_sorts(array<student,2001> &stu, int start, int end,char subject){
    my_sorts(stu,start,end,subject);
    switch (subject){
        case 'A':
            for(int i=start;i<end;i++){
                if(i==0&&stu[i].br>i+1){
                    stu[i].br = i+1;
                    stu[i].subject = subject;
                }
                else if(stu[i].br>i+1){
                    if(stu[i].A==stu[i-1].A){
                        stu[i].br = stu[i-1].br;
                    }
                    else{
                        stu[i].br = i+1;
                    }
                    stu[i].subject = subject;
                }
            }
            break;
        case 'C':
            for(int i=start;i<end;i++){
                if(i==0&&stu[i].br>i+1){
                    stu[i].br = i+1;
                    stu[i].subject = subject;
                }
                else if(stu[i].br>i+1){
                    if(stu[i].C==stu[i-1].C){
                        stu[i].br = stu[i-1].br;
                    }
                    else{
                        stu[i].br = i+1;
                    }
                    stu[i].subject = subject;
                }
            }
            break;
        case 'M':
            for(int i=start;i<end;i++){
                if(i==0&&stu[i].br>i+1){
                    stu[i].br = i+1;
                    stu[i].subject = subject;
                }
                else if(stu[i].br>i+1){
                    if(stu[i].M==stu[i-1].M){
                        stu[i].br = stu[i-1].br;
                    }
                    else{
                        stu[i].br = i+1;
                    }
                    stu[i].subject = subject;
                }
            }
            break;
        case 'E':
            for(int i=start;i<end;i++){
                if(i==0&&stu[i].br>i+1){
                    stu[i].br = i+1;
                    stu[i].subject = subject;
                }
                else if(stu[i].br>i+1){
                    if(stu[i].E==stu[i-1].E){
                        stu[i].br = stu[i-1].br;
                    }
                    else{
                        stu[i].br = i+1;
                    }
                    stu[i].subject = subject;
                }
            }
            break;
    }
}

int main(){
    int N,M;
    scanf("%d%d",&N,&M);
    int n = N;
    array<student,2001> stu;
    while(N--){
        int id,c,m,e;
        scanf("%d%d%d%d",&id,&c,&m,&e);
        stu[N] = student(id,c,m,e);
    }
    use_my_sorts(stu,0,n,'A');
    use_my_sorts(stu,0,n,'C');
    use_my_sorts(stu,0,n,'M');
    use_my_sorts(stu,0,n,'E');
    while(M--){
        int f=0,id;
        scanf("%d",&id);
        for(int i=0;i<n;i++){
            if(stu[i].ID==id){
                f=1;
                printf("%d %c\n",stu[i].br,stu[i].subject);
            }
        }
        if(!f){
            printf("N/A\n");
        }
    }
    return 0;
}
posted @ 2022-01-24 22:59  ghosteq  阅读(39)  评论(0编辑  收藏  举报