1. 程式人生 > >【PAT甲級】1012 The Best Rank

【PAT甲級】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 CME 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

題意分析

這題是每個學生都有自己的id,每個學生還有三種成績和三種成績的平均數,四種成績都有各自的排名,然後給出學生id,求出學生這四種成績排名中的最好排名並輸出。

個人思路

這題考的是排序演算法的應用。

我是建立了一個學生資訊結構體,內部存著四種成績和四種成績排名。

通過四次排序對求出四個成績排名並存入結構體中。

然後設立了一個map,對映著最後一次排序後的學生id和排序索引下標的對映。【目的是為了之後查詢學生排名的時候能在O(logn)的時間內完成查詢,加快查詢速度】

有一個注意點:

99 98 98 97 96 這樣的排名應該是1 2 2 4 5,相同的成績同排名,之後的排名會增加到當前人數。

程式碼實現

#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define eps 1e-8
#define INF 0x7FFFFFFF

const int maxn = 2005;

using namespace std;

// 學生結構體
struct Student {
    string id;
    int c_score, m_score, e_score;
    double a_score;
    int c_rank, m_rank, e_rank, a_rank;
};
Student students[maxn];

// 四個成績的比較函式
bool cmp_c(Student s1, Student s2) {
    return s1.c_score > s2.c_score;
}

bool cmp_m(Student s1, Student s2) {
    return s1.m_score > s2.m_score;
}

bool cmp_e(Student s1, Student s2) {
    return s1.e_score > s2.e_score;
}

bool cmp_a(Student s1, Student s2) {
    return s1.a_score > s2.a_score;
}

// 找到某個學生的最佳排名和排名型別
void find_biggest_rank(Student s, int & rank, char & type) {
    if (s.e_rank <= rank) {
        rank = s.e_rank;
        type = 'E';
    }
    if (s.m_rank <= rank) {
        rank = s.m_rank;
        type = 'M';
    }
    if (s.c_rank <= rank) {
        rank = s.c_rank;
        type = 'C';
    }
    if (s.a_rank <= rank) {
        rank = s.a_rank;
        type = 'A';
    }
}

int main() {
    // 輸入
    int n, m;
    cin >> n >> m;
    // 輸入學生成績並求出平均成績
    for (int i = 0; i < n; i ++) {
        cin >> students[i].id >> students[i].c_score >> students[i].m_score >> students[i].e_score;
        students[i].a_score = 1.0 * (students[i].c_score+students[i].m_score+students[i].e_score) / 3;
    }
    // 求出c語言排名
    sort(students, students+n, cmp_c);
    students[0].c_rank = 1;
    for (int i = 1; i < n; i ++) {
        if (students[i].c_score == students[i-1].c_score) students[i].c_rank = students[i-1].c_rank;
        else students[i].c_rank = i + 1;
    }
    // 求出數學排名
    sort(students, students+n, cmp_m);
    students[0].m_rank = 1;
    for (int i = 1; i < n; i ++) {
        if (students[i].m_score == students[i-1].m_score) students[i].m_rank = students[i-1].m_rank;
        else students[i].m_rank = i + 1;
    }
    // 求出英語排名
    sort(students, students+n, cmp_e);
    students[0].e_rank = 1;
    for (int i = 1; i < n; i ++) {
        if (students[i].e_score == students[i-1].e_score) students[i].e_rank = students[i-1].e_rank;
        else students[i].e_rank = i + 1;
    }
    // 求出平均成績排名
    sort(students, students+n, cmp_a);
    students[0].a_rank = 1;
    for (int i = 1; i < n; i ++) {
        if (students[i].a_score == students[i-1].a_score) students[i].a_rank = students[i-1].a_rank;
        else students[i].a_rank = i + 1;
    }
    // 建立字串到平均成績排序的對映
    map <string, int> idx;
    for (int i = 0; i < n; i ++) {
        idx[students[i].id] = i+1;
    }
    // 查詢m個學生成績
    while (m --) {
        string id;
        cin >> id;
        if (idx[id] == 0) {
            cout << "N/A" << endl;
        }
        else {
            int pos = idx[id]-1;
            int rank = INF;
            char type = '0';
            find_biggest_rank(students[pos], rank, type);
            cout << rank << " " << type << endl;
        }
    }
    return 0;
}

總結

學習不息,繼續加油

感覺這題的程式碼寫得又臭又長,中間還因為粗心一個細節沒注意到找了好久,希望之後能找到比較簡潔明瞭的程式碼學習一下。