冬天给车“搓澡”:技术流的呵护指南
2025/12/25 20:34:26
python
运行
def hanoi(n, start, helper, target): if n == 1: print(f"移动圆盘1从柱子{start}到柱子{target}") return hanoi(n-1, start, target, helper) print(f"移动圆盘{n}从柱子{start}到柱子{target}") hanoi(n-1, helper, start, target) # 读取输入 input_str = input().strip() n = int(input_str.split('=')[1]) # 执行汉诺塔递归 hanoi(n, 'A', 'B', 'C')python
运行
from collections import deque def min_time_catch_cow(N, K): # 边界:农夫在牛右侧,只能左移 if N >= K: return N - K max_pos = 2 * K # 限制最大位置,避免越界 visited = [False] * (max_pos + 2) queue = deque() queue.append((N, 0)) visited[N] = True while queue: curr_pos, time = queue.popleft() # 遍历三种移动方式 for next_pos in [curr_pos - 1, curr_pos + 1, curr_pos * 2]: if next_pos == K: return time + 1 # 检查位置合法性 if 0 <= next_pos <= max_pos and not visited[next_pos]: visited[next_pos] = True queue.append((next_pos, time + 1)) return -1 # 读取输入 N, K = map(int, input().split()) # 输出最小时间 print(min_time_catch_cow(N, K))python
运行
def max_path_sum(): # 读取行数 R = int(input()) # 初始化为最后一行 dp = list(map(int, input().split())) # 从倒数第二行向上递推 for i in range(R-2, -1, -1): current_row = list(map(int, input().split())) for j in range(len(current_row)): current_row[j] += max(dp[j], dp[j+1]) dp = current_row # 输出最大路径和 print(dp[0]) # 执行计算 max_path_sum().py文件上传;n=3;5 17;plaintext
5 13 11 8 12 7 26 6 14 15 8 12 7 13 24 11