D

2026 Spring 第八次上机

public
dsb May 07, 2026 Never 96
Clone
Python Eqs.py
Python Snowflake.py
Python Eqs.py 26 lines (24 loc) | 879 Bytes
1
a1, a2, a3, a4, a5 = map(int, input().split())
2
# 计算前两个值的组合结果并存储在字典中
3
pre_calculated = {}
4
for x1 in range(-50, 51):
5
if x1 != 0:
6
for x2 in range(-50, 51):
7
if x2 != 0:
8
result = a1 * x1 ** 3 + a2 * x2 ** 3
9
if result not in pre_calculated:
10
pre_calculated[result] = 1
11
else:
12
pre_calculated[result] += 1
13
14
count = 0
15
# 进行三重循环
16
for x3 in range(-50, 51):
17
if x3 != 0:
18
for x4 in range(-50, 51):
19
if x4 != 0:
20
for x5 in range(-50, 51):
21
if x5 != 0:
22
target = -(a3 * x3 ** 3 + a4 * x4 ** 3 + a5 * x5 ** 3)
23
if target in pre_calculated:
24
count += pre_calculated[target]
25
26
print(count)
Python Snowflake.py 35 lines (27 loc) | 888 Bytes
1
N_HASH = 10001
2
3
class Snowflake:
4
def __init__(self, nums: list):
5
self.nums = sorted(nums)
6
7
def __eq__(self, value):
8
if not isinstance(value, Snowflake):
9
return NotImplemented
10
11
# given that length of nums in each snowflake is 6
12
for i in range(6):
13
if self.nums[i] != value.nums[i]:
14
return False
15
return True
16
17
def __hash__(self):
18
return hash(tuple(self.nums))
19
20
21
def main():
22
n = int(input())
23
snowflakes = set()
24
for _ in range(n):
25
nums = list(map(int, input().split()))
26
snowflake = Snowflake(nums)
27
if snowflake in snowflakes:
28
print("Twin snowflakes found.")
29
return
30
snowflakes.add(snowflake)
31
print("No two snowflakes are alike.")
32
33
34
if __name__ == "__main__":
35
main()
Python 数字三角形.py 12 lines (10 loc) | 402 Bytes
1
n = int(input())
2
triangle = []
3
for _ in range(n):
4
triangle.append(list(map(int, input().split())))
5
6
# 从倒数第二行开始向上遍历
7
for i in range(n - 2, -1, -1):
8
for j in range(len(triangle[i])):
9
# 选择下方相邻两个数中较大的一个,加上当前位置的数
10
triangle[i][j] += max(triangle[i + 1][j], triangle[i + 1][j + 1])
11
12
print(triangle[0][0])