D

2026 Spring 第四次上机

public
dsb Apr 02, 2026 Never 109
Clone
Python 罗马数字.py 15 lines (13 loc) | 584 Bytes
This code takes an integer input 't', then reads 't' number of integers and converts them into Roman numerals format. It defines lists for different places in a Roman numeral (thousands, hundreds, tens, ones) and populates them with corresponding Roman numeral values. In the loop, for each input number, it calculates the corresponding Roman numeral parts by dividing the number and accessing the index in each list for the respective parts (thousands, hundreds, tens, ones). Once it calculates each part, it concatenates them to form the complete Roman numeral representation of the input number. Finally, it prints the Roman numeral for each input number. Overall, this code snippet efficiently converts a given integer input into its respective Roman numeral representation.
1
t = int(input())
2
3
thousands = ["", "M", "MM", "MMM"]
4
hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
5
tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
6
ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
7
8
for _ in range(t):
9
num = int(input())
10
thoudsands_part = thousands[num // 1000]
11
hundreds_part = hundreds[(num % 1000) // 100]
12
tens_part = tens[(num % 100) // 10]
13
ones_part = ones[num % 10]
14
roman_numeral = thoudsands_part + hundreds_part + tens_part + ones_part
15
print(roman_numeral)
Python 登山.py 25 lines (21 loc) | 665 Bytes
1
n = int(input())
2
height_list = list(map(int, input().split()))
3
4
upper_list = []
5
down_list = []
6
for i in range(n):
7
max_upper = 1
8
for j in range(0,i):
9
if height_list[j] < height_list[i]:
10
max_upper = max(max_upper, upper_list[j]+1)
11
upper_list.append(max_upper)
12
13
for i in range(n-1,-1,-1):
14
max_down = 1
15
for j in range(n-1,i,-1):
16
if height_list[j] < height_list[i]:
17
max_down = max(max_down, down_list[n-1-j]+1)
18
down_list.append(max_down)
19
20
down_list = down_list[::-1]
21
22
max_answer = 0
23
for i in range(n):
24
max_answer = max(max_answer, upper_list[i]+down_list[i]-1)
25
print(max_answer)
Python 约瑟夫问题.py 17 lines (17 loc) | 436 Bytes
1
while True:
2
m , n = map(int, input().split())
3
if m == 0 and n == 0:
4
break
5
monkey_list = []
6
for i in range(m):
7
monkey_list.append(i)
8
index = 0
9
while len(monkey_list) > 1:
10
index = index + 1
11
monkey = monkey_list.pop(0)
12
if index == n:
13
index = 0
14
continue
15
else:
16
monkey_list.append(monkey)
17
print(monkey_list[0] + 1)