1217 字
6 分钟
蓝桥杯python组考前速通笔记
2025-12-11

找到当年蓝桥杯python组考前速通笔记,我当时速通完爽拿了个Python A组国一,应该还是有一点参考价值的!

有一些东西差了点,时间久远忘了当时还差什么了。

我考前准备的速通todo:

  • 排序
  • 栈,队列
  • 优先队列
  • 数学库
  • 对拍
  • 扩大递归深度
  • 时间库
  • ASCII码
  • 输出格式
  • 哈希(忘了为什么当时没勾这个)

蓝桥杯python组感觉主要是有一些和C++不太一样的东西,如果比较少用python写题就容易考场上坐牢。

下面是我考前准备的一些主要的不一样的要点。

零. IDLE配置#

蓝桥杯python指定编译器用python IDLE,这个默认配置不太好用。高亮什么的很奇怪,要自己设置。

image-20250614230110719

Options-Configure IDLE设置代码高亮。

字体我设置的是Cascadia Mono,也可以用几乎每台电脑都有的Consolas

主题在这里改成黑色,或者就白色的也行。

image-20250614230403100

代码补全,有些低版本的IDLE没带,所以我平时准备蓝桥杯时也不开代码补全。就不记录了。推荐准备的时候用考场上一样的配置(也就是先不用vscode,尽可能多用IDLE)。

一. 调试工具#

输入重定向#

对应C++里的freopen

sys.stdin = open("1.txt", "r")

对拍#

一个简单的对拍代码,调用暴力的std.py、测试的未必正确的正解test.py、数据生成器gen.py

import subprocess as sp
import sys
n = 100
for i in range(n):
print(f"test #{i} ", end="")
sp.run(["python", "gen.py"], stdout=open("in.txt", "w"))
sp.run(["python", "std.py"], stdin=open("in.txt","r"), stdout=open("out1.txt", "w"))
sp.run(["python", "test.py"], stdin=open("in.txt","r"), stdout=open("out2.txt", "w"))
with open("out1.txt", "r") as f1, open("out2.txt", "r") as f2:
out1 = f1.read()
out2 = f2.read()
if out1 == out2:
print("pass")
else:
print("failed")
break

二. 基础问题#

排序#

a = sorted(a, key = lambda x: x[1], reverse = True)

日期与时间#

直接看文档

import datetime
help(datetime)

在打印出来的

CLASSES
builtins.object
date
datetime
time
timedelta
tzinfo
timezone

可以看见继承关系。例如datetime继承自date

以这题为例:https://www.lanqiao.cn/problems/16955/learning/

一些函数直接看help。

输出一个整数,表示在公元 190119011111 日到 2000200012123131 日期间,有多少个月的第一天是周日。

from datetime import date, timedelta
s = date(1901, 1, 1)
t = date(2000, 12, 31)
dt = timedelta(days = 1)
last = -1
ans = 0
while s != t:
if last != s.month:
if s.weekday() == 6:
ans += 1
last = s.month
s += dt
print(ans)

还有这题:https://www.lanqiao.cn/problems/2271/learning/

import os
from datetime import date, timedelta
now = date(2022, 1, 1)
ans = 0
while now.year == 2022:
now += timedelta(days=1)
if now.weekday() == 5 or now.weekday() == 6 or now.day in [1, 11, 21, 31]:
ans += 1
print(ans)

以及这题https://www.lanqiao.cn/problems/1452/learning/

import os, time
t = int(input()) // 1000
print(time.strftime("%H:%M:%S", time.gmtime(t)))

输出格式#

类似c++。

# 百分比格式
ratio = 0.75
print(f"{ratio:.1%}") # 输出: 75.0%
# 科学计数法
large_num = 1234567
print(f"{large_num:.2e}") # 输出: 1.23e+06
# 二、八、十六进制
number = 42
print(f"{number:b}") # 输出: 101010 (二进制)
print(f"{number:o}") # 输出: 52 (八进制)
print(f"{number:x}") # 输出: 2a (十六进制小写)
print(f"{number:X}") # 输出: 2A (十六进制大写)

保留小数例题:https://www.lanqiao.cn/problems/8631/learning/

进制转换#

实际上python的int函数是可以用base来指定转换基数的。

例题:https://www.lanqiao.cn/problems/19807/learning

import os
import sys
print(int("10", base=2))

三. 注意事项#

递归深度#

python默认递归深度大概1000,容易爆栈。

查看递归深度

import sys
print(sys.getrecursionlimit())

设置递归深度

import sys
print(sys.setrecursionlimit(3000))

可以help(sys)来查看,感觉这个不好记住。

ASCII码转换#

获取一个字符的ASCII码,可以用ord

ch = 'a'
print(ord(ch))

如果要将一个数值,按照ASCII码,转换成字符,就要用到chr函数。

ch2 = chr(97)
print(ch2)

例题链接:https://www.lanqiao.cn/problems/1446/learning

四. 数据结构#

优先队列:heapq#

python中用heapq库来实现优先队列功能。

例题:https://www.lanqiao.cn/problems/741/learning

import sys, os
# sys.stdin = open("1.txt", "r")
n = int(input())
a = list(map(int, input().split()))
ans = 0
import heapq
que = []
for x in a:
heapq.heappush(que, x)
while len(que) != 1:
u = heapq.heappop(que)
v = heapq.heappop(que)
ans += u + v
heapq.heappush(que, u + v)
print(ans)

双端队列:deque#

用的是collections库的deque

例题:https://www.lanqiao.cn/problems/511/learning

import sys, os
m, n = map(int, input().split())
words = list(map(int, input().split()))
from collections import deque
que = deque()
inque = set()
ans = 0
for w in words:
if w in inque:
continue
ans += 1
if len(que) >= m:
old = que.popleft()
inque.remove(old)
que.append(w)
inque.add(w)
print(ans)

五. 数学库#

python数学库比C++感觉多了不少东西。

组合数函数

factorial(x) - 计算阶乘,直接返回x!

comb(n, k) - 计算组合数C(n,k)C(n,k),即二项式系数

perm(n, k) - 计算排列数P(n,k)P(n,k)

数论函数

gcd(*integers) - 计算多个整数的最大公约数

lcm(*integers) - 计算多个整数的最小公倍数

isqrt(n) - 计算整数平方根(向下取整)

高精度

fsum(seq) - 高精度浮点数求和,避免累积误差

ceil(x)floor(x) - 向上和向下取整

isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) - 判断两个浮点数是否近似相等(处理浮点误差)

除此之外,python内置了快速幂,pow(base, exp[, mod])

# 带模的快速幂(更高效)
result = pow(2, 10, 1000000007) # 计算2^10 % 1000000007
蓝桥杯python组考前速通笔记
https://fuwari.vercel.app/posts/acm/lanqiao-python-before-exam/
作者
wegret
发布于
2025-12-11
许可协议
CC BY-NC-SA 4.0