1012 The Best Rank (25point(s))

目录

首先,先贴柳神的博客

https://www.liuchuo.net/ 这是地址

想要刷好PTA,强烈推荐柳神的博客,和算法笔记

题目原文

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

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

题目大意:

期末考试,考了C语言,数学,英语,总分(这是是要你算的,不是考的)

要你找出学生最好的那门课,如果有学生的课程的排名相同,按照

总分>C语言>数学>英语

代码如下

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
struct Student {
    char id[7]; 
    int c;      //C语言的成绩
    int m;      //数学的成绩
    int e;      //英语的成绩
    int sum;    //总分
    int rank = -1;  //最好的排名
    int rank_class = -1;    //用来存储最好成绩的课程的
};
int main(void) {
    Student Arry[2010];
    char Course[6] = { 'C','M','E','A' };
    char id[7];         //用来存储我们要找的那个学生
    int t;              //这是我们要寻找的那个学生的下标
    int N, M;
    scanf("%d%d", &N, &M);
    for (int i = 0; i < N; i++) {
        scanf("%s%d%d%d",&Arry[i].id, &Arry[i].c, &Arry[i].m, &Arry[i].e);
        Arry[i].sum = Arry[i].c + Arry[i].m + Arry[i].e;
    }
    while (M--) {
        scanf("%s", id);
        t = -1;
        for (int i = 0; i < N; i++) {
            if (strcmp(Arry[i].id, id) == 0) {
                t = i;
                break;
            }
        }
        if (t == -1) {
            printf("N/A");
            if (M != 0) {
                printf("\n");
            }
            continue;
        }
        int rank_c, rank_m, rank_e, rank_s;
        rank_c = rank_m = rank_e = rank_s = 1;      //他们的值都是N
        for (int i = 0; i < N; i++) {
            if (Arry[i].c > Arry[t].c) {
                rank_c++;
            }
            if (Arry[i].m > Arry[t].m) {
                rank_m++;
            }
            if (Arry[i].e > Arry[t].e) {
                rank_e++;
            }
            if (Arry[i].sum > Arry[t].sum) {
                rank_s++;
            }
        }

        if (rank_s <= rank_c) {
            Arry[t].rank_class = 3;
            Arry[t].rank = rank_s;
        }
        else {
            Arry[t].rank_class = 0;
            Arry[t].rank = rank_c;
        }

        if (Arry[t].rank > rank_m) {
            Arry[t].rank_class = 1;
            Arry[t].rank = rank_m;
        }
        if (Arry[t].rank > rank_e) {
            Arry[t].rank_class = 2;
            Arry[t].rank = rank_e;
        }
        printf("%d %c", Arry[t].rank, Course[Arry[t].rank_class]);
        if (M != 0) {
            printf("\n");
        }
    }
    return 0;
}

诺神的代码

#include <cstdio>
#include <algorithm>
using namespace std;
struct node {
    int id, best;
    int score[4], rank[4];  //rank,分别是他们的等级
}stu[2005];
int exist[1000000], flag = -1;
bool cmp1(node a, node b) { return a.score[flag] > b.score[flag]; }
int main() {
    int n, m, id;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
        stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;    //一个四舍五入
    }
    for (flag = 0; flag <= 3; flag++) {
        sort(stu, stu + n, cmp1);       //一个一个的排序
        stu[0].rank[flag] = 1;
        for (int i = 1; i < n; i++) {
            stu[i].rank[flag] = i + 1;
            if (stu[i].score[flag] == stu[i - 1].score[flag])   //如果有成绩相同的办法
                stu[i].rank[flag] = stu[i - 1].rank[flag];
        }
    }
    for (int i = 0; i < n; i++) {
        exist[stu[i].id] = i + 1;
        stu[i].best = 0;
        int minn = stu[i].rank[0];          //就是选出最小的
        for (int j = 1; j <= 3; j++) {
            if (stu[i].rank[j] < minn) {
                minn = stu[i].rank[j];
                stu[i].best = j;
            }
        }
    }
    char c[5] = { 'A', 'C', 'M', 'E' };
    for (int i = 0; i < m; i++) {
        scanf("%d", &id);
        int temp = exist[id];
        if (temp) {     //如果存在的话
            int best = stu[temp - 1].best;
            printf("%d %c\n", stu[temp - 1].rank[best], c[best]);
        }
        else {
            printf("N/A\n");
        }
    }
    return 0;
}

反思:

我的代码没有婼神简介的原因

诺神的代码高度的抽象化,比如她的学科就统一用了一个score数组,我的还是c,m.e,s

她根据题目给的判断优先级,特意的调正了顺序,减少了代码的逻辑量

她还是用到了sort函数,其实我也一直想用这个的,但是没有想到好的用法,这里诺神,再次教了我.

总结

① 尽量写抽象化的代码,减少代码量

② 在可以通过的前提下,用函数是好的

③ 多思考,多记录。

上一篇:1040 Longest Symmetric String (25point(s)) 需要二刷 *动态规划 回文子串问题


下一篇:1045 快速排序 (25point(s))