不忘初心,
牢记使命。

Python解答第十届蓝桥杯大赛个人赛软件类B

2021-04-04 大聪明 0评论 218 0喜欢

组队

problemCB02

class Team:
    def __init__(self, info):
        self.info = info


team = []
ans = 0

with open('../数据/team.txt', mode='r', encoding='utf-8') as f:
    for line in f:
        team.append(Team(list(line.strip().split())))

for i in range(20):
    for j in range(i+1, 20):
        for k in range(j+1, 20):
            for m in range(k+1, 20):
                for n in range(m+1, 20):
                    temp = int(team[i].info[1]) + int(team[j].info[2]) + int(team[k].info[3]) + int(team[m].info[4]) + int(team[n].info[5])
                    if temp > ans:
                        ans = temp

print(ans)
# team.txt
"""
1 97 90 0 0 0
2 92 85 96 0 0
3 0 0 0 0 93
4 0 0 0 80 86
5 89 83 97 0 0
6 82 86 0 0 0
7 0 0 0 87 90
8 0 97 96 0 0
9 0 0 89 0 0
10 95 99 0 0 0
11 0 0 96 97 0
12 0 0 0 93 98
13 94 91  0 0 0
14 0 83 87 0 0
15 0 0 98 97 98
16 0 0 0 93 86
17 98 83 99 98 81
18 93 87 92 96 98
19 0 0 0 89 92
20 0 99 96 95 81
"""

年号字串

problemCB04

# # 进制转换一般都是用除K取余法
# num = 2019
# print(num % 26, num // 26)
# print(77 % 26, 77 // 26)
# print(2 % 26, 2 // 26)
# print(chr(ord('A') + 17 - 1))
# print(chr(ord('A') + 25 - 1))
# print(chr(ord('A') + 2 - 1))
# # BYQ
# # 验算
# # LQ 329
# print(ord('Q')-ord('A')+1 + (ord('L')-ord('A')+1) * 26)  # 329
# # BYQ 2019
# print(ord('Q')-ord('A')+1 + (ord('Y')-ord('A')+1) * 26 + (ord('B')-ord('A')+1) * 26 ** 2)  # 2019
# # 所以提交BYQ
#
# # 2019 转成16进制
# # print(2019 % 16, 2019 // 16)
# # print(126 % 16, 126 // 16)
# # print(3 + 14 * 16 + 7 * 16 ** 2)
# # 7E3

# 最后整理一下代码
year = 2019
lst = []
while year != 0:
    lst.append(chr(ord('A') + year % 26 - 1))
    year = year // 26
# reversed(lst)
lst.reverse()
print(*lst)  # B Y Q

数列求值

同试卷A的数列求值

数的分解

problemCB06

def check(x):
    while x != 0:
        a = x % 10
        if a == 2 or a == 4:
            return True
        x = x // 10
    return False


num = 2019
lst = []
for i in range(1, num + 1):
    if check(i):
        continue
    for j in range(i + 1, num - i + 1):
        if check(j):
            continue
        for k in range(j + 1, num - i - j + 1):
            if check(k):
                continue
            if i + j + k == num:
                lst.append([i, j, k])
# print(lst)
print(len(lst))
# 40785

迷宫

同试卷A的迷宫

特别数的和

problemCB09

# import time
# start = time.time()
n = int(input())
num = 0
# for i in range(1, 10000 + 1):
for i in range(1, n + 1):
    a = i
    while a != 0:
        b = a % 10
        if b in [2, 0, 1, 9]:
            num += i
            break
        a = a // 10
print(num)
# end = time.time()
# print(end - start)
# 10000
# 41951713
# 0.004991292953491211s
# 数据规模不大,直接暴力求解

完全二叉树的权值

同试卷A的完全二叉树的权值

n = int(input())
lst = list(map(int, input().split()))
deep = 1
while 2 ** deep - 1 <= n:
    lst[deep-1] = sum(lst[2**(deep-1)-1:2**deep-1])
    deep += 1
lst[deep] = sum(lst[2**(deep-1)-1:])
print(lst.index(max(lst)) + 1)

等差数列

problemCB12

def get_len():
    n = int(input())
    lst = list(map(int, input().split()))
    count = 1
    lst.sort()
    num = lst[0]
    dif = lst[1] - lst[0]
    for i in range(2, n):
        new_dif = lst[i] - lst[i - 1]
        dif = new_dif if new_dif < dif else dif
    while num < lst[-1]:
        num += dif
        count += 1
    return count


if __name__ == '__main__':
    print(get_len())

"""
注释:
1. sorted 排序
l1 = [22, 33, 1, 2, 7, 4]
l2 = sorted(l1)
# print(l1)  # [22, 33, 1, 2, 7, 4]  不会改变原来的数组内容
# print(l2)   # [1, 2, 4, 7, 22, 33]
l2 = [('太白',18), ('alex', 73), ('wusir', 35), ('口天吴', 41)]
print(sorted(l2))  # [('alex', 73), ('wusir', 35), ('口天吴', 41), ('太白', 18)]
print(sorted(l2, key=lambda x:x[1]))  # 返回的是一个列表  [('太白', 18), ('wusir', 35), ('口天吴', 41), ('alex', 73)]
print(sorted(l2, key=lambda x:x[1], reverse=True))  # 从大到小


2. 列表的sort()方法排序
l1.sort()
print(l1)  # [1, 2, 4, 7, 22, 33] 返回值为None 把原来的列表元素从小到大排序,改变原列表内容
l2 = [('太白',18), ('alex', 73), ('wusir', 35), ('口天吴', 41)]
l2.sort(key=lambda x: x[1])
# print(l2)  # [('太白', 18), ('wusir', 35), ('口天吴', 41), ('alex', 73)]
l2.sort(key=lambda x: x[1], reverse=True)
print(l2)  # [('alex', 73), ('口天吴', 41), ('wusir', 35), ('太白', 18)]  # 从大到小
"""

后缀表达式

灵能传输

发表评论 取消回复

电子邮件地址不会被公开。

请输入正确格式的qq邮箱
请输入以http或https开头的URL,格式如:https://libo_sober.top