2021-09-14

PAT 甲级 1012.The Best Rank (25 分) Python流水账式写法

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.

Input

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

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个学生的ID以及三门课的成绩(C, M, E), 再加上一门平均成绩(三门课分数的平均值),对这四个分数进行排名,所以每个学生会有四个排名。M个查询输出,对于每一个ID,输出当前ID对应的学生最好的排名以及对应成绩,如果某位学生的四个排名中有名次相同,按照A, C, M, E的顺序输出。
注意点:如果某门学科中有学生分数相同,那么并列排名应该是1、1、3、4、5,而不是1、1、2、3、4。

N, M = input().split()
N = int(N)
M = int(M)
c, m, e, a = [], [], [], []
for i in range(N):
    ID, c_score, m_score, e_score = input().split()
    c_score = int(c_score)
    m_score = int(m_score)
    e_score = int(e_score)
    a_score = (c_score + m_score + e_score) / 3.0
    c.append((ID, c_score))
    m.append((ID, m_score))
    e.append((ID, e_score))
    a.append((ID, a_score))
c = sorted(c, key=lambda x: x[1], reverse=True)
m = sorted(m, key=lambda x: x[1], reverse=True)
e = sorted(e, key=lambda x: x[1], reverse=True)
a = sorted(a, key=lambda x: x[1], reverse=True)
c[0] = (c[0][0], 1, c[0][1])
for i in range(1, N):
    if c[i][1] < c[i - 1][2]:
        c[i] = (c[i][0], i + 1, c[i][1])
    elif c[i][1] == c[i - 1][2]:
        c[i] = (c[i][0], c[i - 1][1], c[i][1])
m[0] = (m[0][0], 1, m[0][1])
for i in range(1, N):
    if m[i][1] < m[i - 1][2]:
        m[i] = (m[i][0], i + 1, m[i][1])
    elif m[i][1] == m[i - 1][2]:
        m[i] = (m[i][0], m[i - 1][1], m[i][1])
e[0] = (e[0][0], 1, e[0][1])
for i in range(1, N):
    if e[i][1] < e[i - 1][2]:
        e[i] = (e[i][0], i + 1, e[i][1])
    elif e[i][1] == e[i - 1][2]:
        e[i] = (e[i][0], e[i - 1][1], e[i][1])
a[0] = (a[0][0], 1, a[0][1])
for i in range(1, N):
    if a[i][1] < a[i - 1][2]:
        a[i] = (a[i][0], i + 1, a[i][1])
    elif a[i][1] == a[i - 1][2]:
        a[i] = (a[i][0], a[i - 1][1], a[i][1])
dic = {}
for item in c:
    dic[item[0]] = []
for item in c:
    dic[item[0]].append((item[1], 'C'))
for item in m:
    dic[item[0]].append((item[1], 'M'))
for item in e:
    dic[item[0]].append((item[1], 'E'))
for item in a:
    dic[item[0]].append((item[1], 'A'))
class_dic = {'A': 1, 'C': 2, 'M': 3, 'E': 4}
new_dic = {}
for k, v in dic.items():
    v = sorted(v, key=lambda x: (x[0], class_dic[x[1]]))
    new_dic[k] = v

res = []
for i in range(M):
    id = str(input())
    if id in new_dic.keys():
        res.append(new_dic[id][0])
    else:
        res.append('N/A')
for i in range(len(res) - 1):
    if res[i] != 'N/A':
        print(res[i][0], end=" ")
        print(res[i][1])
    else:
        print(res[i])
if res[-1] != 'N/A':
    print(res[-1][0], end=" ")
    print(res[-1][1])
else:
    print(res[-1])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值