D

2026 Spring 第一次上机

public
dsb Mar 12, 2026 Never 210
Clone
Python 多项式加法.py 42 lines (36 loc) | 1.21 KB
1
def polynomial_add(p1: str, p2: str):
2
p1, p2 = [int(x) for x in p1.split()], [int(x) for x in p2.split()]
3
length1, length2 = len(p1), len(p2)
4
5
poly1, poly2 = {}, {}
6
7
for i in range(length1 // 2):
8
coef, power = p1[2 * i], p1[2 * i + 1]
9
if power < 0:
10
continue
11
poly1.setdefault(power, 0)
12
poly1[power] += coef
13
for i in range(length2 // 2):
14
coef, power = p2[2 * i], p2[2 * i + 1]
15
if power < 0:
16
continue
17
poly2.setdefault(power, 0)
18
poly2[power] += coef
19
20
result_power = [power for power in poly1]
21
for power_ in [power for power in poly2]:
22
if not power_ in result_power:
23
result_power.append(power_)
24
result_power = sorted(result_power, reverse=True)
25
26
result_str = []
27
for power in result_power:
28
coef = 0
29
if power in poly1:
30
coef += poly1[power]
31
if power in poly2:
32
coef += poly2[power]
33
if coef != 0:
34
result_str.append("[ %d %d ]" % (coef, power))
35
return " ".join(result_str)
36
37
38
n = int(input())
39
for i in range(n):
40
p1 = input()
41
p2 = input()
42
print(polynomial_add(p1 ,p2))
Python Number Sequence.py 15 lines (12 loc) | 339 Bytes
This code takes an integer `n` as input and then iterates `n` times. For each iteration, it takes another integer `m` as input. Within each iteration, the code initializes `current_digit` to 1 and `current_string` to "1". It then enters a `while` loop that iterates until `m` is greater than 0. In each iteration of the `while` loop, the code checks if `m` is less than or equal to the length of `current_string`. If it is, the loop breaks. Otherwise, the code subtracts the length of `current_string` from `m`, increments `current_digit` by 1, and appends the new value of `current_digit` to `current_string`. Finally, after exiting the `while` loop, the code prints the character at index `m - 1` in `current_string`. Overall, this code seems to be generating a string pattern based on the input `m` and then outputting a specific character from that string based on the value of `m`.
1
n = int(input())
2
3
for _ in range(n):
4
m = int(input())
5
current_digit = 1
6
current_string = "1"
7
8
while(m > 0):
9
if m <= len(current_string):
10
break
11
m -= len(current_string)
12
current_digit += 1
13
current_string += str(current_digit)
14
15
print(current_string[m - 1])
Python 神奇的幻方.py 31 lines (27 loc) | 710 Bytes
This code generates and prints a magic square of order n (an n x n matrix that has the following properties: the sum of elements in each row, each column, and each diagonal are the same). Let's break down the code: 1. Input the value of n from the user. 2. Calculate the size of the magic square by multiplying n by 2 and then subtracting 1. This will result in an m x m square, where m = 2n - 1. 3. Create a 2D array 'array' of size m x m and fill it with 'None' initially. 4. Initialize variables current_x and current_y to start from position (1, 1). 5. Set count as 1 and total_count as the total number of elements in the square (m * m). 6. Create the magic square by moving in a specific pattern and assigning values to the array based on the position. 7. Finally, print the generated magic square by looping through each row and printing the elements separated by spaces. This code uses a specific algorithm to generate a magic square of odd order. It moves diagonally from the top-right corner to fill the numbers in a spiral inward pattern. The algorithm determines the movement direction based on certain conditions, leading to the generation of a complete magic square. The output of this code will be a printed representation of the generated magic square based on the input value of n provided by the user.
1
n = int(input())
2
m = 2 * n - 1
3
4
array = [[None] * m for i in range(m) ]
5
6
# 从1计数
7
current_x = 1
8
current_y = 1
9
count, total_count = 1, m * m
10
11
while count <= total_count:
12
if count == 1:
13
current_x, current_y = 1, n
14
elif current_x == 1 and current_y == m:
15
current_x += 1
16
elif current_x == 1:
17
current_x = m
18
current_y += 1
19
elif current_y == m:
20
current_y = 1
21
current_x -= 1
22
elif array[current_x - 2][current_y]:
23
current_x += 1
24
else:
25
current_x -= 1
26
current_y += 1
27
array[current_x - 1][current_y - 1] = count
28
count += 1
29
30
for x in array:
31
print(" ".join([str(i) for i in x]))