PAT (Basic Level) Practice (中文)1015 德才论 python

24 篇文章 0 订阅

出处:
https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312

这个题按照题目描述的一步步写出来到没有很难,但是问题是python的常规写法往往会有超时。
在这里插入图片描述

我看到很多地方都是用别的语言解决这个问题,目前只看到一个用python写的可全部通过(多数情况下)的版本:
https://blog.csdn.net/letv0907/article/details/104788854?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162115286416780255298862%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=162115286416780255298862&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allbaidu_landing_v2~default-6-104788854.pc_search_result_before_js&utm_term=1015+德才论+python
原博客也把优化的过程写的很清楚,用到了很多优化的技巧。
这里简单把代码注释一下:

import sys
#导入sys,用sys.stdin.readline()代替input(),sys.stdout.write代替print(),读取大量数据时时间效率会提升很多

#通过将代码放入定义的函数中执行提升效率
def print_result(l):
    for total in l[::-1]:
        if total:
            for moral in total[::-1]:
                if moral:
                    for k in sorted(moral):
                        sys.stdout.write(student.get(k))


def set_result(list, total, moral, num):
#按照题目的要求用多维数组存储
#第一层为学生德才总分,列表长度为0-200。
#德才总分相同时,按得分排序,所以第二层为德分,长度为0-100。
#下一层存放德分相同时的准考证号。
    if list[total] == 0:
        list[total] = [0] * 101
        list[total][moral] = [num]
    elif list[total][moral] == 0:
        list[total][moral] = [num]
    else:
        list[total][moral].append(num)

#建立一个字典,用来将字符串类型的数字转换成int型,比自带的int转换时间快,以空间换时间
score_dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
              '10': 10, '11': 11, '12': 12, '13': 13, '14': 14, '15': 15, '16': 16, '17': 17,
              '18': 18, '19': 19, '20': 20, '21': 21, '22': 22, '23': 23, '24': 24, '25': 25,
              '26': 26, '27': 27, '28': 28, '29': 29, '30': 30, '31': 31, '32': 32, '33': 33,
              '34': 34, '35': 35, '36': 36, '37': 37, '38': 38, '39': 39, '40': 40, '41': 41,
              '42': 42, '43': 43, '44': 44, '45': 45, '46': 46, '47': 47, '48': 48, '49': 49,
              '50': 50, '51': 51, '52': 52, '53': 53, '54': 54, '55': 55, '56': 56, '57': 57,
              '58': 58, '59': 59, '60': 60, '61': 61, '62': 62, '63': 63, '64': 64, '65': 65,
              '66': 66, '67': 67, '68': 68, '69': 69, '70': 70, '71': 71, '72': 72, '73': 73,
              '74': 74, '75': 75, '76': 76, '77': 77, '78': 78, '79': 79, '80': 80, '81': 81,
              '82': 82, '83': 83, '84': 84, '85': 85, '86': 86, '87': 87, '88': 88, '89': 89,
              '90': 90, '91': 91, '92': 92, '93': 93, '94': 94, '95': 95, '96': 96, '97': 97,
              '98': 98, '99': 99, '100': 100}
n = sys.stdin.readline().split()
N = int(n[0])
L, H = score_dict[n[1]], score_dict[n[2]]
student = {}


def main():
    l1, l2, l3, l4 = [0] * 201, [0] * 201, [0] * 201, [0] * 201
    #以学生德才总分为第一层,所以列表长度为0到200。
    for i in range(N):
        input_ = sys.stdin.readline()
        info = input_.split()
        num = info[0]
        moral = score_dict[info[1]]
        talent = score_dict[info[2]]
        total = moral + talent

        if moral >= H and talent >= H:
            set_result(l1, total, moral, num)
            student[num] = input_
        elif moral >= H and talent >= L:
            set_result(l2, total, moral, num)
            student[num] = input_
        elif moral >= talent >= L:
            set_result(l3, total, moral, num)
            student[num] = input_
        elif moral >= L and talent >= L:
            set_result(l4, total, moral, num)
            student[num] = input_

    print(len(student))
    print_result(l1)
    print_result(l2)
    print_result(l3)
    print_result(l4)


main()

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值