PAT乙级练习题_1015“德才论”_python解题

测试点2 3 4时间超时,希望路过的朋友能指点一二

原题

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式

输入第一行给出 3 个正整数,分别为:N(≤ 1 0 5 10^5 ​​ ),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

my answer

思路

用一个变量qualification_number来存储有资格的考生人数
创建四个列表分别存放四类考生的信息
每输入一个考生信息就去判断该考生是否有“资格”,若有资格qualification_number+=1,然后通过if-elif语句对有资格的考生进行分类并将考生信息放到相应的列表中去
输入完成之后,对每一个列表依次按照“总分”“德分”“考生号”进行排序
输出结果

# 在已经按照总分排序的前提下,总分相同的考生按德分进行排序
# 思路:因为在“按总分排序”的前提下,相同德分的考生必定在列表是相邻的。
#      所以用两个变量存储这“一串”相同德分的考生的信息在列表中的起始位置。
#      然后对这“一串“考生信息构成的子列表按照德分进行排序,再将该排完序的子列表重新放回原来的位置

# 在实现上述思路的时候会遇到个问题:
# 子列表的起点如何确定,又该在何时更新?
# 本人解决的办法是,用一个布尔变量进行控制子列表起始位置index_start的变化
# 初始化为True,在从列表0位置逐个遍历整个列表的时候,当发现当前位置考生的德分与下一位考生的德分一致时,就将当前考生的位置赋值给index_start,并将该布尔变量变成false,
# 此后再遇到相同的德分,就只改变子列表的末尾值。
def sort_by_de(ls):
    flag = True
    index_start = 0
    index_end = 0
    for i in range(len(ls)-1):
        if ls[i][3] == ls[i+1][3]:
            if flag:
                index_start = i
                flag = False
            index_end = i+1
        else:
            if index_start == index_end:
                flag = True
                continue
            else:
                tmp = ls[index_start: index_end+1]
                tmp.sort(key=lambda de: de[1], reverse=True)
                ls[index_start: index_end+1] = tmp
                index_start = i+1
                index_end = i+1
                flag = True
        # 在进行循环的时候还会遇到一种特殊情况,即子列表末尾与列表末尾相同。
        # 在这种情况下,本次循环已经到了最后一次了,就不会对该子列表进行排序操作
        # 所以需要将这种特殊情况单独进行处理
        if index_end == len(ls)-1 and index_end != index_start:
            tmp = ls[index_start:]
            tmp.sort(key=lambda de: de[1], reverse=True)
            ls[index_start:] = tmp

# 在按总分排序后,相同总分又按德分排过顺序的前提下,相同德分的考生按照考生号进行排序
# 思路与上面的函数的思路相同
def sort_by_id(ls):
    flag = True
    index_start = 0
    index_end = 0
    for i in range(len(ls)-1):
        if ls[i][1] == ls[i+1][1]:
            if flag:
                index_start = i
                flag = False
            index_end = i+1
        else:
            if index_start == index_end:
                flag = True
                continue
            else:
                tmp = ls[index_start: index_end+1]
                tmp.sort(key=lambda de: de[0], reverse=False)
                ls[index_start: index_end+1] = tmp
                index_start = i+1
                index_end = i+1
                flag = True
        if index_end == len(ls)-1 and index_end != index_start:
            tmp = ls[index_start:]
            tmp.sort(key=lambda de: de[1], reverse=False)
            ls[index_start:] = tmp
# 获取评分标准以及考生人数
input_str = input()
N = int(input_str.split()[0])
L = int(input_str.split()[1])
H = int(input_str.split()[2])
qualification_number = 0 # 有“录取资格”的考生人数
## 用四个列表来存储每一类考生的信息
one_people = [] # 第一类考生,列表中的每一个元素还是一个列表,存储着考生的考号、德分、才分、总分。下同
two_people = [] # 第二类考生
three_people = [] # 第三类考生
four_people = [] # 第四类考生
while N > 0:
    input_info = input()
    temp_list = input_info.split()
    temp_de = int(temp_list[1])
    temp_cai = int(temp_list[2])
    temp_sum = temp_de + temp_cai
    if temp_de < L or temp_cai < L: # 判断该考生是否有录取资格。若没有,则直接跳过本次循环
        N -= 1
        continue
    elif temp_de >= H and temp_cai >= H: # 第一类考生
        qualification_number += 1
        temp_list.append(temp_sum)
        one_people.append(temp_list)
    elif temp_de >= H and temp_cai < H: # 第二类考生
        qualification_number += 1
        temp_list.append(temp_sum)
        two_people.append(temp_list)
    elif temp_de < H and temp_cai < H and temp_de >= temp_cai: # 第三类考生
        qualification_number += 1
        temp_list.append(temp_sum)
        three_people.append(temp_list)
    else: # 其他考生
        qualification_number += 1
        temp_list.append(temp_sum)
        four_people.append(temp_list)
    N -= 1
# 先按照总分进行排序
one_people.sort(key=lambda number: number[3], reverse=True)
# 相同总分的考生再按照德分进行排序
sort_by_de(one_people)
# 相同总分和德分的考生,再按照考号排序
sort_by_id(one_people)

two_people.sort(key=lambda number: number[3], reverse=True)
sort_by_de(two_people)
sort_by_id(two_people)

three_people.sort(key=lambda number: number[3], reverse=True)
sort_by_de(three_people)
sort_by_id(three_people)

four_people.sort(key=lambda number: number[3], reverse=True)
sort_by_de(four_people)
sort_by_id(four_people)

print(qualification_number)
for i in one_people:
    print(i[0], i[1], i[2])
for i in two_people:
    print(i[0], i[1], i[2])
for i in three_people:
    print(i[0], i[1], i[2])
for i in four_people:
    print(i[0], i[1], i[2])
发布了28 篇原创文章 · 获赞 19 · 访问量 5215

猜你喜欢

转载自blog.csdn.net/weixin_43912972/article/details/103998683