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

在这里插入图片描述

第一次写的代码(最后一个测试点会超时):
#本题输入中有一下格式字符串:
#(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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值