天天看點

PAT (Advanced Level) Practice 1012 The Best Rank (25分)

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

這題是PAT甲級的1012題,題目描述為英文,題目的意思為:

1012的最好排名(25分)

為了評估我們第一年CS專業學生的表現,我們隻考慮他們三門課程的成績:C - C程式設計語言,M -數學(微積分或線性代數),和E -英語。同時,我們通過強調學生的最好等級來鼓勵他們——也就是說,在三門課程和平均成績的四個等級中,我們為每個學生列印出最好的等級。

例如,4名學生的C、M、E、A -的平均成績如下:

學生ID C M E A

310101 98 85 88 90

310102 70 95 88 84

310103 82 87 94 88

310104 91 91 91 91

所有學生的最好成績排名第一,因為第一名的C語言成績最好,第二名的數學成績最好,第三名的英語成績最好,最後一名的平均成績最好。

輸入規格:

每個輸入檔案包含一個測試用例。每個案例都以一行開始,其中包含兩個數字N和M(≤2000),這兩個數字分别是學生總數和檢查其排名的學生人數。然後是N行,每一行包含一個學生ID,它是一個6位數的字元串,後面是該學生的三個整數等級(範圍為[0,100]),順序為C、M和e。然後是M行,每一行包含一個學生ID。

輸出規範:

對于每一位M名學生,列印出他/她的最佳排名,以及相應排名的符号,用空格隔開。

排序方法的優先級按 A>C>M>E排序。是以,如果一個學生有兩種或兩種以上的方法來獲得相同的最佳排名,則輸出優先級最高的方法。

如果一個學生不在評分名單上,簡單地輸出N/A。

示例輸入:

輸出示例:

#include<iostream>
#include <algorithm>
using namespace std;
int judge[1000005];
struct Rank{
    int id;
    int grade[4];
    int rank[4];
    int best;
}student[2005];
int i = -1;
string ss = "ACME";
bool cmp(Rank a,Rank b)
{
    return a.grade[i]>b.grade[i];
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>student[i].id>>student[i].grade[1]>>student[i].grade[2]>>student[i].grade[3];
        student[i].grade[0]=int((student[i].grade[1]+student[i].grade[2]+student[i].grade[3])/3.0+0.5);
    }
    for(i = 0; i < 4; i++){//對學生進行排名,i控制每次排序的成績
        sort(student,student+n,cmp);
        student[0].rank[i]=1;
        for(int j = 1; j < n; j++)
        {
            student[j].rank[i]=j+1;
            if(student[j].grade[i]==student[j-1].grade[i]) student[j].rank[i]=student[j-1].rank[i];
        }
    }
    for(int i = 0; i < n; i++){
        judge[student[i].id] = i + 1;
        int minv=student[i].rank[0];
        student[i].best=0;
        for(int j=1;j<=3;j++)
        {
            if(student[i].rank[j]<minv)
            {
                minv=student[i].rank[j];
                student[i].best=j;
            }
        }
    }
    for(int i=0;i<m;i++)
    {
        int id;
        cin>>id;
        if(judge[id]==0) cout<<"N/A"<<endl;
        else{
            int temp=judge[id]-1;
            int best = student[temp].best;
            printf("%d %c\n",student[temp].rank[best],ss[best]);
        }
    }
    return 0;
}