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:

StudentIDCMEA
31010198858890
31010270958884
31010382879488
31010491919191

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值