第十五届蓝桥杯python组真题
时间: 2025-03-07 09:17:04 浏览: 58
### 第十五届蓝桥杯 Python 组 真题
#### A题:穿越时空之门
此题要求设计一个算法来解决特定条件下的时间旅行问题[^2]。
```python
def time_travel(gates, start_time, end_time):
"""
计算从start_time到end_time通过一系列gate的时间消耗.
参数:
gates : List[Tuple[int, int]]
每个gate表示为(start_gate_time, travel_duration).
start_time : int
开始时间戳.
end_time : int
结束时间戳.
返回:
float: 总共花费的时间.
"""
current_time = start_time
total_cost = 0
for gate_start, duration in gates:
if current_time >= end_time:
break
if current_time < gate_start:
wait_time = gate_start - current_time
current_time += wait_time
total_cost += wait_time
current_time += duration
total_cost += duration
remaining_time = max(0, end_time - current_time)
total_cost += remaining_time
return round(total_cost, 2)
gates_data = [(10, 5), (20, 8)]
print(time_travel(gates_data, 5, 30))
```
上述代码实现了`time_travel`函数,用于模拟从给定起始时刻到达目标时刻所需经历的时间成本计算过程。这里假设存在若干个“时空之门”,每个门都有固定的开启时间和传送耗时;程序会依次遍历这些门并累加实际经过的时间直到达到终点为止。
#### C题:拼正方形
该题目涉及几何图形构建逻辑,具体实现如下所示[^3]:
```python
import math
def find_side(a, b):
"""寻找能够构成的最大边长."""
total_area = 4 * a + b
max_side = int(math.sqrt(total_area))
for n in range(max_side, 0, -1):
if (n * n - b) % 4 == 0 and (n * n - b) // 4 <= a:
return n
a_value = 7385137888721
b_value = 10470245
max_square_side = find_side(a_value, b_value)
print(f"The maximum side length of square is {max_square_side}")
```
这段代码定义了一个名为`find_side`的方法,用来找出可以组成最大面积正方形的一条边的长度。输入参数分别为两个整数值`a`和`b`,代表某种资源的数量关系。方法内部先求得总面积,再尝试找到满足条件的最大可能边长返回作为结果输出。
阅读全文
相关推荐














