欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PTA 1073 多选题常见计分法——Python

程序员文章站 2024-03-22 08:10:52
...
改进思路及心得:

利用集合中的交集、并集、差集等运算得到学生与正确答案之间的差别。这题适合用运算是因为对于一道题中的每个选项都是唯一的,而且题目说了没有不合法情况。
以后做题遇到困难可以多想想集合适不适合。不要一味得用列表。

改进后的代码(多运行几次就可以过了):
#本题输入中有一下格式字符串:
#(2 a c) (3 b d e) (2 a c) (3 a b e)
#可以将其头和末尾的半个括号去掉:
#2 a c) (3 b d e) (2 a c) (3 a b e
#如此,它们之间是按') ('分割的

# 获取学生人数(N)。获取题目总数(M)
N,M = map(int,input().split())

#正确答案
answer = []
#错误答案统计
problemcount = {}
for i in range(1,M+1):
    problemcount[i] = 0
for i in range(M):
    temp = input().split()
    temp = temp[0:3] + [set(temp[3:])]
    problemcount[i+1] = {chr(j + ord('a')):0 for j in range(int(temp[1]))}
    answer.append(temp)

#获取学生答案
for i in range(N):
    #学生总成绩
    score = 0
    temp = [j.split() for j in input()[1:-1].split(') (')]
    for j in range(M):
        temp[j] = temp[j][:1] + [set(temp[j][1:])]
        if temp[j][1] == answer[j][3]:
            score += int(answer[j][0])
        elif temp[j][1] < answer[j][3]:
            score += int(answer[j][0]) / 2
            for k in (answer[j][3] - temp[j][1]):
                problemcount[j+1][k] += 1
        else:
            for k in ((answer[j][3] | temp[j][1]) - (answer[j][3] & temp[j][1])):
                problemcount[j+1][k] += 1
    print('{:.1f}'.format(float(score)))

#对错误答案次数排序
for i in problemcount:
    problemcount[i] = sorted(problemcount[i].items(),key = lambda x:-x[1])
Max = 0
#获得错误最多次数的值
for i in problemcount:
    if Max < problemcount[i][0][1]:
        Max = problemcount[i][0][1]
if Max == 0:
    print('Too simple')
else:
    for i in problemcount:
        for j in problemcount[i]:
            if j[1] == Max:
                print(Max,str(i) + '-' + j[0])
            else:
                break

PTA 1073 多选题常见计分法——Python

第一次写的代码(最后一个测试点会超时):
#本题输入中有一下格式字符串:
#(2 a c) (3 b d e) (2 a c) (3 a b e)
#可以将其头和末尾的半个括号去掉:
#2 a c) (3 b d e) (2 a c) (3 a b e
#如此,它们之间是按') ('分割的

N,M = map(int,input().split())
answer = []
problemcount = {}
for i in range(1,M+1):
    problemcount[i] = 0
for i in range(M):
    temp = input().split()
    problemcount[i+1] = {chr(j + ord('a')):0 for j in range(int(temp[1]))}
    answer.append(temp)
for i in range(N):
    temp = [j.split() for j in input()[1:-1].split(') (')]
    score = 0
    for j in range(M):
        flag = True
        for k in range(1,int(temp[j][0]) + 1):
            if temp[j][k] not in answer[j]:
                problemcount[j+1][temp[j][k]] += 1
                flag =False
        for k in range(3,int(answer[j][2]) + 3):
            if answer[j][k] not in temp[j]:
                problemcount[j+1][answer[j][k]] += 1
        if flag:
            if answer[j][2] == temp[j][0]:
                score += int(answer[j][0])
            else:
                score += int(answer[j][0]) / 2
    print('{:.1f}'.format(float(score)))

for i in problemcount:
    problemcount[i] = sorted(problemcount[i].items(),key = lambda x:-x[1])
Max = 0
for i in problemcount:
    if Max < problemcount[i][0][1]:
        Max = problemcount[i][0][1]
if Max == 0:
    print('Too simple')
else:
    for i in problemcount:
        for j in problemcount[i]:
            if j[1] == Max:
                print(Max,str(i) + '-' + j[0])
            else:
                break

相关标签: python 算法超时