蓝桥杯备赛指南
蓝桥杯备赛指南
大学C组
枚举
from enum import Enum
class Weekday(Enum):
monday = 1
tuesday = 2
wednesday = 3
thirsday = 4
friday = 5
saturday = 6
sunday = 7
print(Weekday.wednesday) # Weekday.wednesday
print(type(Weekday.wednesday)) # <enum 'Weekday'>
print(Weekday.wednesday.name) # wednesday
print(Weekday.wednesday.value) # 3
排序
冒泡排序
def bubble_sort_with_flag(nums:list[int]) -> list[int]:
n = len(nums)
for i in range(n-1, 0, -1):
flag = False
for j in range(0, i):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nusm[j+1], nums[j]
flags = True
if not flag:
break
return nums
选择排序
def selection_sort(nums:list[int]) -> list[int]:
n = len(nums)
for i in range(n):
k = i
for j in range(i+1, n):
if nums[k] > nums[j]:
k = j
nums[i], nums[k] = nums[k], nums[i]
return nums
插入排序
def insertion_sort(nums:list[int]) -> list[int]:
n = len(nums)
for i in range(1, n):
base = nums[i]
j = i-1
while j >= 0 and nums[j] > base:
nums[j+1] = nums[j]
j -= 1
nums[j] = nums[base]
return nums
搜索
BFS
def graph_bfs(graph:GraphAdjList, start_vet:Vertex) -> list[Vertex]:
res = []
visited = {start_vet}
que = deque(start_vet)
while que > 0:
vet = que.popleft()
res.append(vet)
for adj_vet in garpg.adj_list:
if adj_vet in visited:
countinue
visited.add(adj_vet)
que.append(adj_vet)
return res
DFS
def dfs(graph:GraphAdjList, visited:{Vertex}, res:list[Vertex], vet:Vertex):
'''DFS辅助函数'''
res.append(vet)
visited.add(vet)
for adj_vet in graph.adj_list:
if adj_vet in visited:
continue
dfs(graph, visited, res, adj_vet)
def graph_dfs(graph:GraphAdjList, start_vet:Vertex) -> list[Vertex]:
'''DFS主函数'''
res = []
visited = {start_vet}
dfs(graph, visited, res, start_vet)
return res
贪心算法
-
零钱兑换(局部最优)示例:
零钱兑换:在一堆钱币中找到最接近要求限制的最大钱币总额。
def coin_change_greedy(coins:list[int], amt:int) -> int: i = len(coins) - 1 count = 0 while amt > 0: while i >0 and coins[i] > amt: i -= 1 amt -= coins[i] count += 1 return count if amt == 0 else -1
-
分数背包示例:
选择一组物品,使得总重量不超过载重,且总价值最大。(可以选择物品的一部分,价值根据选择的重量的比例计算)
class Item: '''物品''' def __init__(self, w:int, v:int): self.w = w # 物品重量 self.v = v # 物品价值 def fractional_knapsack(wgt:list[int], val:list[int], cap:int) -> int: '''分数背包:贪心''' items = [items(w, v) for w, v in zip(wgt, val)] items.sort(key=lambda item: item.v / item.w, reverse=True) res = 0 for item in items: if item.w <= cap: res += item.v cap -= item.w else: res += (item.v / item.w) * cap return res
-
最大容量问题示例:
通过移动短板最大化两个短板之间的容量(其容量主要在于其两板的短板和两板间距离)。
def max_capacity(ht:list[int]) -> int: i, j = 0, len(ht) - 1 res = 0 while i < j: cap = min(ht[i], ht[j]) * (j - i) res = max(res, cap) if ht[i] < ht[j]: i += 1 else: j -= 1 return res
-
最大切分乘积问题示例:
给定一个正整数,将其切分为至少两个正整数的和,使得切分后所有整数的乘积最大(数学证明可得,大于等于4的数都应该被切分)。
def max_product_cutting(n:int) -> int: if n <= 3: return 1 * (n - 1) a, b = n // 3, n % 3 if b == 1: return int(math.pow(3, a-1)) * 2 *2 if b == 2: return int(math.pow())
模拟
-
螺旋矩阵:
def spiralOrder(matrix:list[list[int]]) -> list[int]: res = [] left = 0 right = len(matrix[0]) top = 0 bottom = len(matrix) while right > left and bottom > top: res.extend(matrix[top][left:right]) top += 1 res.extend([matrix[i][right-1] for i in range(top, bottom)]) right -= 1 if top < bottom: res.extend(matrix[bottom-1][left:right][::-1]) bottom -= 1 if left < right: res.extend([matrix[j][left] for j in range(bottom-1, top-1, -1)]) left += 1 return res