【PAT甲级】1012 The Best Rank (25 分)

1012 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

思路

本题比较恶心,最简单的想法是根据A,C,M,E分别排出所有人的排名,然后对于某一个学生,找到A,C,M,E排名最大的输出即可。

难点:由于第二步需要根据ID查找,因此将学生的排名存在map<string,int>中,将每一个学生的排名放在map数据结构中,方便查找

首先定义node结构体,同时要定义compare函数来定义数组的排序方法

struct node
{
    
    
    string ID;
    int score;
};


bool compare(node a,node b)
{
    
    
    return a.score>b.score;
}

在main函数中,先依次将学生的A,C,M,E成绩存在四个vector数组中

	int n,m;
    string temp;
    cin>>n>>m;
    vector<node> A,C,M,E;
    for(int i=0;i<n;i++)
    {
    
    
        cin>>temp;
        node tep;
        tep.ID=temp;
        cin>>tep.score;C.push_back(tep);
        cin>>tep.score;M.push_back(tep);
        cin>>tep.score;E.push_back(tep);
        tep.score=(C[i].score+M[i].score+E[i].score)/3;A.push_back(tep);
    }

接下来对对每个数组按照准则进行排序,同时将每一个学生的排名放在map中,注意相同成绩的排名相同

	map<string,int> orderA,orderC,orderM,orderE;
	sort(A.begin(),A.end(),compare);//排序
    int rank=0,before=-1;
    for(auto p=A.begin();p!=A.end();p++)
    {
    
    
        if(p->score!=before)
        {
    
    
            rank=int(p-A.begin()+1);
            before=p->score;
        }
        orderA[p->ID]=rank;//与前面的成绩相同则排名相同
    }
    //下面同上处理C
    sort(C.begin(),C.end(),compare);
    rank=0;before=-1;
    for(auto p=C.begin();p!=C.end();p++)
    {
    
    
        if(p->score!=before)
        {
    
    
            rank=int(p-C.begin()+1);
            before=p->score;
        }
        orderC[p->ID]=rank;
    }
    //下面同上处理M
    sort(M.begin(),M.end(),compare);
    rank=0;before=-1;
    for(auto p=M.begin();p!=M.end();p++)
    {
    
    
        if(p->score!=before)
        {
    
    
            rank=int(p-M.begin()+1);
            before=p->score;
        }
        orderM[p->ID]=rank;
    }
    //下面同上处理E
    sort(E.begin(),E.end(),compare);
    rank=0;before=-1;
    for(auto p=E.begin();p!=E.end();p++)
    {
    
    
        if(p->score!=before)
        {
    
    
            rank=int(p-E.begin()+1);
            before=p->score;
        }
        orderE[p->ID]=rank;
    }

最后查找成绩排名,输出最好的成绩

	for(int i=0;i<m;i++)
    {
    
    
        cin>>temp;
        int bestrank=INF;
        int rA,rC,rM,rE;
        //这四个变量分别存储四个排名
        rA=orderA[temp];rC=orderC[temp];rM=orderM[temp];rE=orderE[temp];
        bestrank=min(rA,bestrank);
        bestrank=min(rC,bestrank);
        bestrank=min(rM,bestrank);
        bestrank=min(rE,bestrank);
        if(bestrank==0)//如果没有排名,证明这个学生不存在
            cout<<"N/A"<<endl;
        else//输出最好的一个
        {
    
    
            if(bestrank==rA)
                cout<<bestrank<<" A"<<endl;
            else if(bestrank==rC)
                cout<<bestrank<<" C"<<endl;
            else if(bestrank==rM)
                cout<<bestrank<<" M"<<endl;
            else
                cout<<bestrank<<" E"<<endl;
        }
    }

git仓库:The Best Rank

猜你喜欢

转载自blog.csdn.net/qq_35779286/article/details/96199427