[ PAT-A ] 1012 The Best Rank (C++)

题目描述

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 Algebra), 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
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
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


解题思路

题目大意为,现已知n个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。k个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的分数,如果名次相同,按照A>C>M>E的顺序输出。如果当前id不存在,输出N/A
对于PAT官网上测试的数据可以很轻松的通过,一是对于学生id应为6位的字符串,而测试数据中全为数字,这样一来,即使id不为string类型亦可通过,二是对于分数,尤其对平均分而言,应该是含有小数的,所以处理时定义double或者float,都可通过.
对于牛客网上测试的数据就比较严格了,一是学生id必为字符串类型,二是没有小数之说,即使平均分可能含有小数,也一律按int型处理.
(以上心得,天知道多少次调试过后才得出的~~)
题目本身只是一道水题,重点是上面的雷区,注意到了将很轻松AC
可以将单课成绩和平均成绩分别进行排序,查询时只需遍历一下,查找排名考前的科目即可.如果担心这样暴力可能会超时的话(事实并不会),可用下面的设计方案.


代码设计
//部分代码
//zhicheng
const int maxn=2000+5;
string id;
int a[maxn][4];
map<int,int> p[4],re[4];
map<string,int> fg;
int main()
{
    int n,m;
    int cnt=1;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        cin>>id;
        for(int j=1;j<4;j++) scanf("%d",&a[i][j]);
        a[i][0]=(a[i][1]+a[i][2]+a[i][3])*1.0/3;
        fg[id]=cnt++;
        for(int j=0;j<4;j++) p[j][a[i][j]]++;
    }
    for(int i=0;i<4;i++)
    {
        int ans=1;
        for(map<int,int>::reverse_iterator it=p[i].rbegin();it!=p[i].rend();it++)
        {
            re[i][it->first]=ans;
            ans+=it->second;
        }    
    }
    for(int i=0;i<m;i++)
    {
        cin>>id;
        if(!fg[id]){printf("N/A\n");continue;}
        int A,C,M,E;
        A=re[0][a[fg[id]-1][0]];
        C=re[1][a[fg[id]-1][1]];
        M=re[2][a[fg[id]-1][2]];
        E=re[3][a[fg[id]-1][3]];
        if(A<=C&&A<=M&&A<=E) printf("%d A\n",A);
        if(C<A&&C<=M&&C<=E) printf("%d C\n",C);
        if(M<C&&M<A&&M<=E) printf("%d M\n",M);
        if(E<A&&E<M&&E<C) printf("%d E\n",E);
    }
    clear();
    return 0;
}


有关PAT (Basic Level) 的更多内容可以关注 ——> PAT-B题解


有关PAT (Advanced Level) 的更多内容可以关注 ——> PAT-A题解

铺子日常更新,如有错误请指正
传送门:代码链接 PTA题目链接 牛客网题目链接 PAT-A题解

猜你喜欢

转载自blog.csdn.net/S_zhicheng27/article/details/81271881