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 Algebra), 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.


  • 輸入格式
    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.

  • 輸出格式
    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”.


解題方法:
這裏的我採用的是map+sort的解法:先對學生這個結構體通過不同的課程進行排序,每按一個類別排完序後,就相應的更新該類別的rank。當所有類別的課程都排序完成後,用學生ID做key,學生結構體做value,放入map中。
這樣就可以方便地進行判斷是否該key存在,以及快速地定位到該ID對應的學生結構體。


程序:

#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <algorithm>
using namespace std;
int CLASS = -1;
char C[5] = {'A', 'C', 'M', 'E'};
struct stu
{
    int ID;
    int score[4], rank[4];
};

int cmp(stu s1, stu s2)
{   /* 降序排序 */
    return s1.score[CLASS] > s2.score[CLASS];
}

int main(int argc, char const *argv[])
{
    int N, M, ID, bestRank, bestRankClass;
    stu S[2001];
    map <int, stu> MAP;
    scanf("%d %d", &N, &M);
    for (int i = 0; i < N; i++)
    {
        scanf("%d %d %d %d", &S[i].ID, &S[i].score[1], &S[i].score[2], &S[i].score[3]);
        S[i].score[0] = (S[i].score[1] + S[i].score[2] + S[i].score[3]) / 3.0 + 0.5; /* 四捨五入 */
    }
    for (CLASS = 0; CLASS <= 3; CLASS++)
    {   /* 按課程進行排序 */
        sort(S, S+N, cmp);
        S[0].rank[CLASS] = 1;   /* 給排第一的學生賦值 */
        for (int i = 1; i < N; i++)
        {   /* 如果此學生跟前面學生評分一樣 */
            if (S[i].score[CLASS] == S[i-1].score[CLASS])
                S[i].rank[CLASS] = S[i-1].rank[CLASS];
            else    /* 如果不一樣 */
                S[i].rank[CLASS] = i + 1;
        }
    }
    for (int i = 0; i < N; i++)
        MAP[S[i].ID] = S[i];    /* 將結構體數組導入進Map中去 */
    for (int i = 0; i < M; i++)
    {
        scanf("%d", &ID);
        if (MAP.find(ID) == MAP.end())  /* 如果沒有該學號 */
            printf("N/A\n");
        else
        {
            bestRank = 2000, bestRankClass = 0;
            for (int j = 0; j <= 3; j++)
                if (MAP[ID].rank[j] < bestRank)
                {   /* 尋找高排名 */
                    bestRankClass = j;
                    bestRank = MAP[ID].rank[j];
                }
            printf("%d %c\n", bestRank, C[bestRankClass]);  
        }
    }
    return 0;
}

如果對您有幫助,幫忙點個小拇指唄~

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