D

2026 Spring 第十次上机

public
dsb May 14, 2026 Never 107
Clone
Python Shell排序.py 50 lines (41 loc) | 1.21 KB
1
def shell_sort_with_hisbard_sequence(arr):
2
n = len(arr)
3
4
# 生成 Hibbard 增量序列:2^k - 1
5
gaps = []
6
k = 1
7
while True:
8
gap = (2 ** k) - 1
9
if gap >= n:
10
break
11
gaps.append(gap)
12
k += 1
13
# 反转,使得增量从大到小
14
gaps.reverse()
15
16
# 存储每趟排序后的结果
17
result = []
18
19
# 对每个增量进行一趟排序
20
for gap in gaps:
21
# 对每个子序列进行直接插入排序
22
for i in range(gap, n):
23
temp = arr[i]
24
j = i
25
# 在当前子序列中进行插入排序
26
while j >= gap and arr[j - gap] > temp:
27
arr[j] = arr[j - gap]
28
j -= gap
29
arr[j] = temp
30
31
# 保存当前趟的结果
32
result.append(arr.copy())
33
34
return result
35
36
# 主程序
37
def main():
38
# 输入
39
n = int(input())
40
arr = list(map(int, input().split()))
41
42
# 进行 Shell 排序
43
result = shell_sort_with_hisbard_sequence(arr)
44
45
# 输出每趟排序后的结果
46
for row in result:
47
print(' '.join(map(str, row)))
48
49
if __name__ == "__main__":
50
main()
Python 古代密码.py 26 lines (21 loc) | 629 Bytes
1
def can_transform(encrypted, original):
2
# 统计两个字符串中各字母的出现频率
3
freq1 = [0] * 26
4
freq2 = [0] * 26
5
6
for c in encrypted:
7
freq1[ord(c) - ord('A')] += 1
8
for c in original:
9
freq2[ord(c) - ord('A')] += 1
10
11
# 对频率数组排序后比较
12
freq1.sort()
13
freq2.sort()
14
15
# 如果排序后的频率完全相同,则可以通过替换+排列得到
16
return freq1 == freq2
17
18
# 输入
19
encrypted = input().strip()
20
original = input().strip()
21
22
# 输出
23
if can_transform(encrypted, original):
24
print("YES")
25
else:
26
print("NO")
Python 小木棍.py 65 lines (55 loc) | 2.08 KB
This code implements a recursive algorithm to solve a problem involving sticks and a target length. The main idea is to check if it is possible to form the target length using the sticks provided as input. Here is an analysis of the code: 1. The `solve` function is defined to recursively check if it's possible to form the target length with the sticks. It takes various parameters like the sticks list, number of sticks, total length, current length, visited array, number of used sticks, and current index. 2. The function uses backtracking to try out different combinations of sticks to form the target length. It checks certain conditions like if the current length equals the target length, the sum of sticks is divisible by the target, and if all sticks have been used correctly. 3. The function uses a nested loop to iterate over the sticks and apply certain rules to eliminate invalid combinations before proceeding with recursion. 4. The main while loop reads input, sorts the sticks list in descending order to optimize the search, and then iterates through the possible target lengths to find the minimum target length that can be formed. 5. The code uses backtracking to explore possible combinations and find the solution efficiently. If a valid combination is found, it prints the target length and breaks out of the loop. Overall, the code efficiently solves the problem by exploring different combinations of sticks to form the target length using a recursive approach.
1
# Check if it's possible to form the target length using the sticks
2
def solve(sticks, n, total_length, current_length,
3
visited, num_used_sticks, current_index):
4
5
# Conditions for exiting the recursion
6
if num_used_sticks == n and current_length == 0:
7
return True
8
9
if total_length % target != 0:
10
return False
11
12
# Try to use each stick
13
for i in range(current_index, n):
14
# 1. Rules and prunning
15
if visited[i]:
16
continue
17
if current_length + sticks[i] > target:
18
continue
19
# If the first stick does not fit, it directly implies overall failure
20
if num_used_sticks == 0 and i != 0:
21
return False
22
if i > 0 and sticks[i] == sticks[i - 1] and not visited[i - 1]:
23
continue
24
25
# 2. Now we can use this stick, modify global states
26
visited[i] = True
27
num_used_sticks += 1
28
current_length += sticks[i]
29
if current_length == target:
30
current_length = 0
31
32
# 3. Recursively solve the problem
33
result = solve(sticks, n, total_length, current_length,
34
visited, num_used_sticks,
35
current_index = i + 1 if current_length != 0 else 0)
36
if result:
37
return True
38
39
# 4. Restore global states
40
if current_length == 0:
41
current_length = target - sticks[i]
42
else:
43
current_length -= sticks[i]
44
visited[i] = False
45
num_used_sticks -= 1
46
47
# Don't forget this.
48
return False
49
50
while True:
51
n = int(input())
52
if n == 0:
53
break
54
sticks = list(map(int, input().split()))
55
total_length = sum(sticks)
56
max_stick = max(sticks)
57
58
visited = [False] * n
59
# Important: This optimization decreases the number of branches of search tree
60
sticks = sorted(sticks, reverse=True)
61
62
for target in range(max_stick, total_length + 1):
63
if solve(sticks, n, total_length, 0, visited, 0, 0):
64
print(target)
65
break