PAT甲级刷题记录——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 C, M, Eand 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, Mand 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号,让你输出这个学生在三门功课以及平均成绩这四个序列当中最好的名次以及这门课的名称。因此,在写结构体的时候,最好还要将最好的名次和对应的功课写进去(BestRank和BestCourse)。其他的话,按照步骤来就行了,每次对一门课的成绩进行降序排序(因为是降序,所以要写四个cmp函数),排完一门功课之后,再对每个学生的信息进行处理,即时更新一下每个学生这门课的排名。

这里有一个【坑点】:如果成绩相同,那么他们的名次也相同,即:91、90、88、88、84,对应的名次应该是:1、2、3、3、5。因此,在更新当前学生这门课的排名时,还应该记录上一个学生的成绩和排名,如果上一个学生这门课的成绩和当前学生这门课的成绩相同,那么就需要把上一个学生的排名直接赋值给当前学生的排名。

另外,如果一个学生多门课的排名相同,还应该按A>C>M>E的优先级进行比较输出最高功课的排名,因此,在更新BestRank时,应该按照A、C、M、E这个顺序进行更新,这样的话,即使后面的排名与前面的相同,也不会覆盖掉前面的。

最后,因为更新每个学生的BestRank是一个比较的过程,而BestRank本身是一个结构体的元素,如果不进行初始化的话,那么就会出现错误,因此,这题一定要写构造函数进行初始化(我这里是把所有值都置为了INF,即:一个很大的数)。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int INF = 123123123;
struct student{
    string ID;
    int C, M, E, A;
    int CRank, MRank, ERank, ARank;
    int BestRank;
    char BestCourse;
    student(){
        ID.clear();
        C = M = E = A = CRank = MRank = ERank = ARank = BestRank = INF;
        BestCourse = '\0';
    }
};
vector<student> List, CList, MList, EList, AList;
bool cmpC(student a, student b){
    return a.C>b.C;
}
bool cmpM(student a, student b){
    return a.M>b.M;
}
bool cmpE(student a, student b){
    return a.E>b.E;
}
bool cmpA(student a, student b){
    return a.A>b.A;
}
int main()
{
    int N, M;
    cin>>N>>M;
    for(int i=0;i<N;i++){
        string tmpID;
        int tmpC, tmpM, tmpE;
        cin>>tmpID>>tmpC>>tmpM>>tmpE;
        int tmpA = (tmpC+tmpM+tmpE)/3;
        student tmp;
        tmp.ID = tmpID;
        tmp.C = tmpC;
        tmp.M = tmpM;
        tmp.E = tmpE;
        tmp.A = tmpA;
        List.push_back(tmp);
    }
    sort(List.begin(), List.end(), cmpC);
    int LastScore = 0, LastRank = 0;
    for(int i=0;i<List.size();i++){
        if(i==0){
            List[i].CRank = i+1;
            LastScore = List[i].C;
            LastRank = List[i].CRank;
        }
        else{
            if(List[i].C==LastScore){
                List[i].CRank = LastRank;
            }
            else{
                List[i].CRank = i+1;
                LastScore = List[i].C;
                LastRank = List[i].CRank;
            }
        }
    }
    sort(List.begin(), List.end(), cmpM);
    for(int i=0;i<List.size();i++){
        if(i==0){
            List[i].MRank = i+1;
            LastScore = List[i].M;
            LastRank = List[i].MRank;
        }
        else{
            if(List[i].M==LastScore){
                List[i].MRank = LastRank;
            }
            else{
                List[i].MRank = i+1;
                LastScore = List[i].M;
                LastRank = List[i].MRank;
            }
        }
    }
    sort(List.begin(), List.end(), cmpE);
    for(int i=0;i<List.size();i++){
        if(i==0){
            List[i].ERank = i+1;
            LastScore = List[i].E;
            LastRank = List[i].ERank;
        }
        else{
            if(List[i].E==LastScore){
                List[i].ERank = LastRank;
            }
            else{
                List[i].ERank = i+1;
                LastScore = List[i].E;
                LastRank = List[i].ERank;
            }
        }
    }
    sort(List.begin(), List.end(), cmpA);
    for(int i=0;i<List.size();i++){
        if(i==0){
            List[i].ARank = i+1;
            LastScore = List[i].A;
            LastRank = List[i].ARank;
        }
        else{
            if(List[i].A==LastScore){
                List[i].ARank = LastRank;
            }
            else{
                List[i].ARank = i+1;
                LastScore = List[i].A;
                LastRank = List[i].ARank;
            }
        }
    }
    for(int i=0;i<List.size();i++){
        if(List[i].ARank<List[i].BestRank){
            List[i].BestRank = List[i].ARank;
            List[i].BestCourse = 'A';
        }
        if(List[i].CRank<List[i].BestRank){
            List[i].BestRank = List[i].CRank;
            List[i].BestCourse = 'C';
        }
        if(List[i].MRank<List[i].BestRank){
            List[i].BestRank = List[i].MRank;
            List[i].BestCourse = 'M';
        }
        if(List[i].ERank<List[i].BestRank){
            List[i].BestRank = List[i].ERank;
            List[i].BestCourse = 'E';
        }
    }
    for(int i=0;i<M;i++){
        string tmpID;
        cin>>tmpID;
        bool flag = false;
        for(int i=0;i<List.size();i++){
            if(List[i].ID==tmpID){
                flag = true;
                cout<<List[i].BestRank<<" "<<List[i].BestCourse<<'\n';
                break;
            }
        }
        if(flag==false) cout<<"N/A\n";
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值