PTA甲級——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

Code:

/*1012
排序
*/
#include<bits/stdc++.h>
using namespace std;
int n,m,stuID[2005],best_Rank;
char symbol;
struct Student{
    int ID;
    int C;
    int M;
    int E;
    int A;
    int A_Rank;
    int C_Rank;
    int M_Rank;
    int E_Rank;
}stu[2005];
int cmp_A(Student s1,Student s2){
    return s1.A>s2.A;
}
int cmp_C(Student s1,Student s2){
    return s1.C>s2.C;
}
int cmp_M(Student s1,Student s2){
    return s1.M>s2.M;
}
int cmp_E(Student s1,Student s2){
    return s1.E>s2.E;
}
void Choose_Best_Rank(int k){//選擇最好的排名
    if(stu[k].A_Rank<=stu[k].C_Rank&&stu[k].A_Rank<=stu[k].M_Rank&&stu[k].A_Rank<=stu[k].E_Rank){
        best_Rank=stu[k].A_Rank;
        symbol='A';
    }
    else if(stu[k].C_Rank<=stu[k].A_Rank&&stu[k].C_Rank<=stu[k].M_Rank&&stu[k].C_Rank<=stu[k].E_Rank){
        best_Rank=stu[k].C_Rank;
        symbol='C';
    }
    else if(stu[k].M_Rank<=stu[k].A_Rank&&stu[k].M_Rank<=stu[k].C_Rank&&stu[k].M_Rank<=stu[k].E_Rank){
        best_Rank=stu[k].M_Rank;
        symbol='M';
    }
    else if(stu[k].E_Rank<=stu[k].A_Rank&&stu[k].E_Rank<=stu[k].M_Rank&&stu[k].E_Rank<=stu[k].C_Rank){
        best_Rank=stu[k].E_Rank;
        symbol='E';
    }
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>stu[i].ID>>stu[i].C>>stu[i].M>>stu[i].E;
        stu[i].A=round((stu[i].C+stu[i].M+stu[i].E)*1.0/3);
    }
    sort(stu,stu+n,cmp_A);//按A從小到大排序
    stu[0].A_Rank=1;
    for(int i=1;i<n;i++){
        if(stu[i].A!=stu[i-1].A)stu[i].A_Rank=i+1;
        else stu[i].A_Rank=stu[i-1].A_Rank;
    }
    sort(stu,stu+n,cmp_C);//按C從小到大排序
    stu[0].C_Rank=1;
    for(int i=1;i<n;i++){
        if(stu[i].C!=stu[i-1].C)stu[i].C_Rank=i+1;
        else stu[i].C_Rank=stu[i-1].C_Rank;
    }
    sort(stu,stu+n,cmp_M);//按M從小到大排序
    stu[0].M_Rank=1;
    for(int i=1;i<n;i++){
        if(stu[i].M!=stu[i-1].M)stu[i].M_Rank=i+1;
        else stu[i].M_Rank=stu[i-1].M_Rank;
    }
    sort(stu,stu+n,cmp_E);//按E從小到大排序
    stu[0].E_Rank=1;
    for(int i=1;i<n;i++){
        if(stu[i].E!=stu[i-1].E)stu[i].E_Rank=i+1;
        else stu[i].E_Rank=stu[i-1].E_Rank;
    }
    for(int i=0;i<m;i++)cin>>stuID[i];
    for(int i=0;i<m;i++){
        int j;
        for(j=0;j<n;j++){
            if(stuID[i]==stu[j].ID){
                Choose_Best_Rank(j);
                cout<<best_Rank<<" "<<symbol<<endl;
                break;
            }
        }
        if(j==n)cout<<"N/A"<<endl;
    }
    return 0;
}

一開始我在給定排名時相同分數的排名沒有相同,導致有兩個樣例過不了,還有計算平均成績A時沒有采用四捨五入round函數,直接取了整數部分,計算出來的數值不準確

Wrong Code:

/*1012
排序
*/
#include<bits/stdc++.h>
using namespace std;
int n,m,stuID[2005],best_Rank;
char symbol;
struct Student{
    int ID;
    int C;
    int M;
    int E;
    int A;
    int A_Rank;
    int C_Rank;
    int M_Rank;
    int E_Rank;
}stu[2005];
int cmp_A(Student s1,Student s2){
    return s1.A>s2.A;
}
int cmp_C(Student s1,Student s2){
    return s1.C>s2.C;
}
int cmp_M(Student s1,Student s2){
    return s1.M>s2.M;
}
int cmp_E(Student s1,Student s2){
    return s1.E>s2.E;
}
void Choose_Best_Rank(int k){
    if(stu[k].A_Rank<=stu[k].C_Rank&&stu[k].A_Rank<=stu[k].M_Rank&&stu[k].A_Rank<=stu[k].E_Rank){
        best_Rank=stu[k].A_Rank;
        symbol='A';
    }
    else if(stu[k].C_Rank<=stu[k].A_Rank&&stu[k].C_Rank<=stu[k].M_Rank&&stu[k].C_Rank<=stu[k].E_Rank){
        best_Rank=stu[k].C_Rank;
        symbol='C';
    }
    else if(stu[k].M_Rank<=stu[k].A_Rank&&stu[k].M_Rank<=stu[k].C_Rank&&stu[k].M_Rank<=stu[k].E_Rank){
        best_Rank=stu[k].M_Rank;
        symbol='M';
    }
    else if(stu[k].E_Rank<=stu[k].A_Rank&&stu[k].E_Rank<=stu[k].M_Rank&&stu[k].E_Rank<=stu[k].C_Rank){
        best_Rank=stu[k].E_Rank;
        symbol='E';
    }
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>stu[i].ID>>stu[i].C>>stu[i].M>>stu[i].E;
        stu[i].A=(stu[i].C+stu[i].M+stu[i].E)/3;
    }
    sort(stu,stu+n,cmp_A);
    for(int i=0;i<n;i++)stu[i].A_Rank=i+1;
    sort(stu,stu+n,cmp_C);
    for(int i=0;i<n;i++)stu[i].C_Rank=i+1;
    sort(stu,stu+n,cmp_M);
    for(int i=0;i<n;i++)stu[i].M_Rank=i+1;
    sort(stu,stu+n,cmp_E);
    for(int i=0;i<n;i++)stu[i].E_Rank=i+1;
    for(int i=0;i<m;i++)cin>>stuID[i];
    for(int i=0;i<m;i++){
        int j;
        for(j=0;j<n;j++){
            if(stuID[i]==stu[j].ID){
                Choose_Best_Rank(j);
                cout<<best_Rank<<" "<<symbol<<endl;
                break;
            }
        }
        if(j==n)cout<<"N/A"<<endl;
    }
    for(int i=0;i<n;i++){
        cout<<stu[i].A_Rank;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章