PAT——甲级1012:The Best Rank

1012 The Best Rank (25 point(s))

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 CM 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

这个题倒是不难,但是挺复杂的。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Student
{
    char id[6];
    int A, C, M, E;//每个同学的分数
    int A_rank, C_rank, M_rank, E_rank;//每个分数的排名
}stu[100010];
bool cmp_A(Student a, Student b) { return a.A > b.A; }//这四个函数是排序的时候用的,分别对四个成绩排序。
bool cmp_C(Student a, Student b) { return a.C > b.C; }
bool cmp_M(Student a, Student b) { return a.M > b.M; }
bool cmp_E(Student a, Student b) { return a.E > b.E; }
void print_max(int a, int c, int m, int e) {//这个函数打印出来最高排名。a c m e 分别代表的是各成绩排名。
    if (a <= c && a <= m && a <= e)printf("%d A\n", a);//注意这里的=,符合优先级A>C>M>E
    else if (c < a&&c <= m && c <= e)printf("%d C\n", c);
    else if (m < a&&m < c &&  m <= e)printf("%d M\n", m);
    else printf("%d E\n", e);
}
int main() {
    int m, n;
    scanf("%d%d", &n, &m);
    for (int i = 0;i < n;i++) {
        scanf("%s%d%d%d", 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);//对A成绩排序
    stu[0].A_rank = 1;
    for (int i = 1;i < n;i++) { //对A成绩排名
        if (stu[i].A == stu[i - 1].A)
            stu[i].A_rank = stu[i - 1].A_rank;
        else
            stu[i].A_rank = i + 1;
    }
    //对C成绩排序,排名
    sort(stu, stu + n, cmp_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 = stu[i - 1].C_rank;
        else
            stu[i].C_rank = i + 1;
    }
    //对M成绩排序,排名
    sort(stu, stu + n, cmp_M);
    for (int i = 0;i < n;i++)  stu[i].M_rank = i + 1;
    stu[0].M_rank = 1;
    for (int i = 1;i < n;i++) {
        if (stu[i].M == stu[i - 1].M)
            stu[i].M_rank = stu[i - 1].M_rank;
        else
            stu[i].M_rank = i + 1;
    }
    //对E成绩排序,排名
    sort(stu, stu + n, cmp_E);
    for (int i = 0;i < n;i++)  stu[i].E_rank = i + 1;
    stu[0].E_rank = 1;
    for (int i = 1;i < n;i++) {
        if (stu[i].E == stu[i - 1].E)
            stu[i].E_rank = stu[i - 1].E_rank;
        else
            stu[i].E_rank = i + 1;
    }
    for (int i=0;i < m;i++) {
        char M_ID[6];
        bool flag = false;
        scanf("%s", M_ID);
        for (int  j = 0; j < n; j++)
        {
            if (strcmp(M_ID, stu[j].id) == 0){//寻找这个同学的信息。
                print_max(stu[j].A_rank, stu[j].C_rank, stu[j].M_rank, stu[j].E_rank);
                flag = true;
                break;
            }
        }
        if (!flag) printf("N/A\n");

    }
    return 0;
}

我这个可以改进一下,把id当int储存,题目中说了是6位数字。

并且后面处理查找ID的时候,复杂度比较高,算法笔记里面是申请了rank[10000000][4]这么大的数组。储存学生的排名信息,直接用ID去访问。但我总觉得申请这么大的内存,好虚呀,牺牲空间换时间。

其他我们的思路差不多。先储存,再分别排序,再对排序结果选择最高的输出。

这道题有个坑!

题目里面也没说,就是相同分数是怎么算排名。

比如91,90,88,88,84应该算作1,2,3,3,5。切记不要算作1,2,3,3,4。

原文地址:https://www.cnblogs.com/albert-yzp/p/10145417.html

时间: 2024-08-05 16:21:52

PAT——甲级1012:The Best Rank的相关文章

pat甲级1012

1012 The Best Rank (25)(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 ti

[PTA] PAT(A) 1012 The Best Rank (25 分)

目录 Problem Solution Analysis Code Problem portal: 1012 The Best Rank (25 分) Solution Analysis ?一名学生有三门科目,以及计算出的一个平均成绩,每一个成绩都会有一个排名,现在想要让你输出某一个同学最高的排名(四种成绩排名的最高),以及对应的科目 ?如果给定的同学的四种课程排名的名次信息已经存在,那么就很简单,在里面找到最小的名次,以及对应的科目输出即可. ?可以创建四个数组,数组的每个元素存储某一门科目的

PAT:1012. The Best Rank (25) AC

#include<stdio.h> #include<algorithm> using namespace std; struct Student { int mID; int grade[4]; //0对应平均A,1对应C,2对应M,3对应E }STU[2010]; char course[4]={'A','C','M','E'}; //所有的存储都对应ACME int Rank[10000000][4]={0}; //每个学号四个成绩对应的排名 int now=0; //排序的

PAT(A) 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 e

PAT Advanced 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 e

PAT甲级1012题解——选择一种合适数据存储方式能使题目变得更简单

题目分析: 本题的算法并不复杂,主要是要搞清楚数据的存储方式(选择一种合适的方式存储每个学生的四个成绩很重要)这里由于N的范围为10^6,故选择结构体来存放对应下标为学生的id(N只有2000的范围,所以结构体开10^6其实有点浪费空间),再者对于每个学生的每种成绩的排名我们通过下面的这种方式可以巧妙得到(而且单科成绩是会出现重复的,即并列的情况): 获取排名的方法:其实很简单,由于会出现并列单科成绩的方式,且单个学生的成绩是存储在结构体里的,通过多次排序的方式去解决这道题目是不太合适的,这里用

PAT 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 Algebra), and E - English. At the mean time, w

1012. The Best Rank (25)——PAT (Advanced Level) Practise

题目信息: 1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Math

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu