[PYTHON](PAT)1012 THE BEST RANK (25)

版权声明:首发于 www.amoshuang.com https://blog.csdn.net/qq_35499060/article/details/82055812

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

题目大意

已知N名学生的三门课程成绩,要求求出他们的最好名次,他们的最好名次是各科成绩排名取最优。

然后给定M个查询,输出对应人员的最好名次以及这个名次所对应的课程。

分析

因为要存储的东西比较多,所以定义了一个学生类来存放信息,不然东西实在太多了。

使用字典存放student的实例,方便后面输出的时候找寻该实例。循环获取出入实例化student对象,存放入students字典。

使用sorted函数配合lambda匿名函数排序所有实例,为每个实例的每门课程排序:首先设置第一个成员排名为i+1(第一个元素为1),之后检查该实例该门课程的分数是否和上一个实例该课程的分数相等,如果相等则和上一个实例该门课程的排名相同。

之后循环获取查询信息,使用zip函数将课程名和课程排名整合在一起,在相同排名下,为了防止课程名称影响排名使得成绩并非按照A>C>M>E的顺序,所以在课程名上分别加上序号。

循环输出最小的排名以及该排名的课程名。(^-^)V

之后循环获取输入,输出相应的答案

Python实现

class student:
    def __init__(self, C, M, E, ID):
        self.grade = [round((C+M+E)/3),C,M,E]
        self.id = ID
        self.rank = [-1, -1, -1, -1]        

def main():
    line = input().split(" ")
    n = int(line[0])
    m = int(line[1])
    students = {}
    for x in range(n):
        line = input().split(" ")
        ID = line[0]
        C = int(line[1])
        M = int(line[2])
        E = int(line[3])
        s = student(C, M, E, ID)
        students[ID]=s
    for x in range(4):
        p = sorted(students.values(), key=lambda i : -i.grade[x])
        p[0].rank[x] = 1
        for i in range(1, n):
            p[i].rank[x] = i + 1
            if p[i].grade[x] == p[i-1].grade[x] :
                p[i].rank[x] = p[i-1].rank[x]
    for x in range(m):
        line = input()
        try:
            unit = students[line]
            temp = zip(unit.rank, ['0A','1C','2M','3E'])
            temp = sorted(temp)
            print(str(min(unit.rank)), temp[0][1][-1])
        except:
            print('N/A')
    

if __name__ == "__main__":
    main()

更多技术干活,有趣文章点这里没错

猜你喜欢

转载自blog.csdn.net/qq_35499060/article/details/82055812