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;
}

如果对您有帮助,帮忙点个小拇指呗~

猜你喜欢

转载自blog.csdn.net/invokar/article/details/80482931