D

2026 Spring 第二次上机

public
dsb Mar 18, 2026 Never 103
Clone
Python 完美立方.py 8 lines (8 loc) | 261 Bytes
1
n = int(input())
2
lst = []
3
for a in range(2,n+1):
4
for b in range(2,a):
5
for c in range(b,a):
6
for d in range(c,a):
7
if a ** 3 == b ** 3 + c ** 3 + d **3:
8
print(f'Cube = {a}, Triple = ({b},{c},{d})')
Python Monkeys Pride.py 28 lines (23 loc) | 958 Bytes
This code snippet reads a series of input values which represent coordinates in a 2D plane, and then determines the number of "kings" that can exist in a line. A king is defined as a point whose y-coordinate is greater than all points to its right on the same line. Here is a breakdown of the code: 1. The code enters an infinite while loop and reads an integer input `n` at the beginning of each iteration. 2. If the input `n` is 0, the loop breaks. 3. Within each iteration, a dictionary named `largest_y` is initialized to store the maximum y-coordinate for each unique x-coordinate. 4. For each `n`, the code reads x and y values of points, updates the `largest_y` dictionary to store the maximum y-coordinate for each x-coordinate, and also checks if the current y-coordinate is greater than the stored maximum for that x-coordinate, updating it if necessary. 5. The x-coordinates present in the `largest_y` dictionary are extracted and sorted in descending order. 6. A count variable is initialized to 0 which will keep track of the number of kings on the line. 7. A variable `current_y` is set to one less than the maximum y-coordinate of the first x-coordinate present in the sorted `positions_x` list. 8. The code then iterates over each x-coordinate in descending order. If the y-coordinate of the current x-coordinate is greater than `current_y`, it is considered a king and the count is incremented. The `current_y` is updated to be the maximum of the current y-coordinate and the previous `current_y`. 9. Finally, the count of kings for the current line is printed. Overall, this code calculates the number of "king" points in a line given a series of input points with coordinates.
1
while True:
2
n = int(input())
3
if n == 0:
4
break
5
6
# 首先,给定x=x_0,这条线上最多只有一个king,也就是y最大的那一个
7
# y最大的那一个,为了成为king,他的y值必须大于右侧的所有点
8
# 用一个数组记录x位置上的最大y值,然后从右向左遍历
9
10
largest_y = {}
11
for i in range(n):
12
x, y = [int(x) for x in input().split()]
13
largest_y.setdefault(x, y)
14
if y > largest_y[x]:
15
largest_y[x] = y
16
17
positions_x = [x for x in largest_y]
18
positions_x = sorted(positions_x, reverse=True)
19
20
# 遍历到每一个x位置上的最大y值,只要大于current_y就可以成为king
21
count = 0
22
current_y = largest_y[positions_x[0]] - 1
23
for x in positions_x:
24
if largest_y[x] > current_y:
25
count += 1
26
current_y = max(current_y, largest_y[x])
27
28
print(count)
Python 合格的字符串.py 36 lines (33 loc) | 993 Bytes
This code defines a function called `judge` that evaluates whether a query string fits a given template. The template can contain placeholders marked by using square brackets `[ ]`, which can match any character within the brackets. The function iterates through the query string and template string character by character, comparing them, and ensuring that placeholders in the template match any character in the query string. After defining the `judge` function, the code then asks for an integer input (`count`), then takes `count` number of queries as input and a template string. It then iterates over the queries, checks if each query matches the template using the `judge` function, and prints the index of the query along with the query if it matches the template. The template is converted to lowercase for case-insensitive comparison within the `judge` function. Overall, this code seems to be a pattern matching program that checks if the provided queries match a specific template with placeholders.
1
def judge(query, template):
2
i = 0
3
j = 0
4
flag = True
5
while i < len(query):
6
# 如果这一位不是通配,正常匹配
7
if template[j] != '[':
8
if query[i] != template[j]:
9
flag = False
10
break
11
12
# 如果这一位是通配,把template位置移动到右括号,并记录通配的内容
13
else:
14
content = []
15
j += 1
16
while template[j] != ']':
17
content.append(template[j])
18
j += 1
19
if query[i] not in content:
20
flag = False
21
break
22
i += 1
23
j += 1
24
if j != len(template):
25
flag = False
26
return flag
27
28
count = int(input())
29
queries = []
30
for i in range(count):
31
queries.append(input())
32
template = input().lower()
33
34
for i in range(1, count + 1):
35
if judge(queries[i - 1].lower(), template):
36
print("%d %s" % (i, queries[i - 1]))