2021第十二届蓝桥杯Python组
2022/2/5 12:42:41
本文主要是介绍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括号序列
这篇关于2021第十二届蓝桥杯Python组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门