pat 乙类 1058 python

注意一、使用lstrip()和rstrip()方法可以删除字符串左右两端指定的字符(默认空格)(返回还是字符串),再使用split()方法分割字符串(返回列表)

注意二、使用循环迭代创建列表

n,m = list(map(int,input().split()))
opt_list = []
score_list = [0 for i in range(n)]
err = [0 for i in range(m)]
for i in range(m):
    opt = input()
    opt_list.append(opt)
for i in range(n):
    student = input().rstrip(")").lstrip("(").split(") (")

    for j in range(len(student)):
        if student[j] == opt_list[j][4:]:
            score_list[i] += int(opt_list[j][0])
        else:
            err[j] += 1

for i in score_list:
    print(i)
err_max = max(err)
if err_max == 0:
    print("Too simple")
else:
    print(err_max,end=' ')
    err_count = err.count(err_max)
    for i in range(err_count):
        max_index = err.index(err_max)
        if i != err_count-1:
            print(max_index+1,end=' ')
        else:
            print(max_index+1)
        err[max_index] = -1




简化输出,使用查找的方法输出题目编号

n,m = list(map(int,input().split()))
opt_list = []
score_list = [0 for i in range(n)]
err = [0 for i in range(m)]
for i in range(m):
    opt = input()
    opt_list.append(opt)
for i in range(n):
    student = input().rstrip(")").lstrip("(").split(") (")

    for j in range(len(student)):
        if student[j] == opt_list[j][4:]:
            score_list[i] += int(opt_list[j][0])
        else:
            err[j] += 1

for i in score_list:
    print(i)
err_max = max(err)
if err_max == 0:
    print("Too simple")
else:
    result = str(err_max)
    for i in range(m):
        if err[i] == err_max:
            result += " "+str(i+1)
    print(result)




有一个测试点答案错误,没有使用rstrip()和lstrip()方法,没有考虑多选题个数为一的情况,导致分割出现了错误

n,m = list(map(int,input().split()))
opt_list = []
score_list = [0 for i in range(n)]
err = [0 for i in range(m)]
for i in range(m):
    opt = input()
    opt_list.append(opt)
for i in range(n):
    student = input().split(") (")
    if student[0][1:] == opt_list[0][4:]:
        score_list[i] += int(opt_list[0][0])
    else:
        err[0] += 1
    for j in range(1,len(student)-1):
        if student[j] == opt_list[j][4:]:
            score_list[i] += int(opt_list[j][0])
        else:
            err[j] += 1
    if student[-1][:-1] == opt_list[-1][4:]:
        score_list[i] += int(opt_list[-1][0])
    else:
        err[-1] += 1
for i in score_list:
    print(i)
err_max = max(err)
if err_max == 0:
    print("Too simple")
else:
    print(err_max,end=' ')
    err_count = err.count(err_max)
    for i in range(err_count):
        max_index = err.index(err_max)
        if i != err_count-1:
            print(max_index+1,end=' ')
        else:
            print(max_index+1)
        err[max_index] = -1






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值