蓝桥杯备赛-比赛常用模块

# 1.用于求前缀和
from itertools import accumulate
lis = [1, 2, 3, 1]
print(list(accumulate(lis)))

print("==============================")

# 2.普通队列
from collections import deque
q = deque()
q.append(1) # 队尾入队
q.append(2)
q.append(3)
q.appendleft(0) # 队首入队
print(len(q)) # 判断队列非空
print(q.popleft()) # 队首出队
print(q.pop()) # 队尾出队

print("===============================")

# 3.优先队列(本质上是小根堆)
from queue import PriorityQueue
q = PriorityQueue()
q.put(3) # 入队
q.put(1)
q.put(2)
print(q.empty()) # 判断队列非空
print(q.get()) # 出队(每次出队的为最值)

print("================================")

# 4.堆(默认小根堆, 将元素取负就可以建大根堆)
import heapq
a = [1, 2, 3, 4, 5]
heapq.heapify(a) # 将a建成小根堆
heapq.heappush(a, 8) # 入堆
print(heapq.heappop(a)) # 出堆(最小值出)
print(heapq.heappushpop(a, 0)) # 出堆的同时入堆

print("=================================")

# 5.数学模块
import math
print(math.ceil(math.log(10 ** 5, 2))) # ceil向上取整, log对2取对数
print(math.gcd(12, 4)) # 最大公因数

print("==================================")

# 6.日期模块
from datetime import datetime, timedelta
time = datetime(2024, 4, 6, 12, 0, 0)
print(time) # 2024-04-06 12:00:00
delta = timedelta(weeks=1, days=1, hours=1, minutes=1, seconds=1) # 时间间隔
print(time + delta) # 2024-04-14 13:01:01

# 7.sys模块
import sys
sys.setrecursionlimit(10000) # 设置递归深度
input = sys.stdin.readline # 简化输入

# 8.记忆化搜索模块
from  functools import lru_cache
@lru_cache(maxsize = None)
def dfs():
  pass

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好无聊啊,烦死

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值