编程语言
首页 > 编程语言> > 2021第十二届蓝桥杯Python组

2021第十二届蓝桥杯Python组

作者:互联网

A卡片

在这里插入图片描述

cards = [2021]*10
i = 1
while True:
    s = str(i)
    for j in s:
        cards[int(j)] -= 1
    flag = False
    for j in cards:
        if j < 0:
            flag = True
            break
    if flag:
        break
    i += 1
print(i-1)
# 3181

B直线

在这里插入图片描述

cnt = 0
dots = [(i, j) for i in range(20) for j in range(21)]  # 生成所有点
lines = set()
for i in range(len(dots)):
    for j in range(i+1, len(dots)):
        if dots[j][0]-dots[i][0] != 0:
            x1, x2 = dots[i][0], dots[j][0]
            y1, y2 = dots[i][1], dots[j][1]
            k = (y2-y1)/(x2-x1)
            b = (y1*(x2-x1)-x1*(y2-y1))/(x2-x1)  # 这么写不会炸精度
            lines.add((k, b))
print(len(lines)+20)

# 40257

C货物摆放

在这里插入图片描述

from math import *

n = 2021041820210418
cnt = 0
fac = []
for i in range(1, int(sqrt(n))+1):
    if n % i == 0:
        fac.append(i)
        fac.append(n//i)

for i in fac:
    for j in fac:
        for k in fac:
            if i*j*k == n:
                cnt += 1

print(cnt)
# 2430

D路径

在这里插入图片描述

from math import *

n = 2021
vis = [False]*(n+1)
dis = [float('inf')]*(n+1)
graph = [[float('inf') for i in range(n+1)] for j in range(n+1)]
for i in range(1, n+1):
    for j in range(1, n+1):
        if abs(i-j) > 21:
            continue
        graph[i][j] = lcm(i, j)

dis[1] = 0
while True:
    point = 0
    for i in range(1, n+1):
        if (not vis[i]) and dis[i] < dis[point]:
            point = i
    if point == 0:
        break

    vis[point] = True
    for i in range(1, n+1):
        if dis[i] > dis[point]+graph[point][i]:
            dis[i] = dis[point]+graph[point][i]
print(dis[n])
# 10266837

E回路计数

在这里插入图片描述

F时间显示

在这里插入图片描述

time = int(input())
time %= 24*60*60*1000
h = time//(60*60*1000)
time -= h*60*60*1000
m = time//(60*1000)
time -= m*60*1000
s = time//1000
print("{:0>2d}:{:0>2d}:{:0>2d}".format(h, m, s))

G杨辉三角形

在这里插入图片描述

def generate(tri):
    tmp = [1]
    for i in range(len(tri)-1):
        tmp.append(tri[i]+tri[i+1])
    tmp.append(1)
    return tmp


n = int(input())
res = 0
tri = [1]
while True:
    if n in tri:
        res += tri.index(n)+1
        break
    res += len(tri)
    tri = generate(tri)
print(res)
# 运行超时,40分

H左孩子右兄弟

请添加图片描述
可能有以下 3 种 (这里只列出 3 种,并不是全部) 不同的 “左孩子右兄弟” 表示:
在这里插入图片描述

n = int(input())
tree = [[] for i in range(n+1)]
for i in range(2, n+1):
    tree[int(input())].append(i)


def dfs(i):
    if len(tree[i]) == 0:
        return 0
    maxn = 0
    for j in tree[i]:
        maxn = max(maxn, dfs(j))
    return len(tree[i])+maxn


print(dfs(1))
# 90分

I异或数列

在这里插入图片描述

J括号序列

在这里插入图片描述

标签:tri,dots,point,Python,60,蓝桥,range,2021,dis
来源: https://blog.csdn.net/m0_51507437/article/details/122777777