1. 统计最大值和最小值之间的数字数量

问题描述

给定一个整数列表,统计位于最大值和最小值之间的数字数量。

代码

1
2
3
4
5
6
7
8
9
nums = eval(input())

nummax = max(nums)
nummin = min(nums)
cnt = 0
for i in nums:
if nummin < i < nummax:
cnt += 1
print(cnt)

2. 方程解的计数

问题描述

统计满足以下条件的整数解 (x, y, z) 的数量:

  • a ≤ x, y, z ≤ b
  • z = (100 - 2x - 3y) / 5

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a, b = map(int, input().split())
cnt = 0

for x in range(a, b + 1):
r = 100 - 2 * x
y_min, y_max = a, b

for y in range(y_min, y_max + 1):
z = r - 3 * y
if z % 5 == 0:
z //= 5
if a <= z <= b:
cnt += 1
print(cnt)

3. 统计数字1的出现次数

问题描述

统计从 1n 的数字中,数字 1 出现的次数。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
n = int(input())
cnt = 0
flag = 1
while flag <= n:
l = n % flag
c = (n // flag) % 10
h = n // (flag * 10)
if c == 0:
cnt += h * flag
elif c == 1:
cnt += h * flag + l + 1
else:
cnt += (h + 1) * flag
flag *= 10
print(cnt)

4. 特殊数字查找

问题描述

查找在 1n 范围内能被 12 整除或包含子串“12”的数字。

代码

1
2
3
4
5
6
7
8
9
10
11
n = int(input())
cnt = 0
l = []
for i in range(1, n + 1):
stri = str(i)
if i % 12 == 0:
l.append(i)
elif '12' in stri:
l.append(i)
for i in l:
print(i, end=" ")

5. 回文时间查找

问题描述

找到给定时间之前和之后最近的回文时间。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def is_palindrome_time(time_str):
h, m = time_str.split(":")
return h == m[::-1]

def format_time(h, m):
return f"{int(h)}:{int(m)}"

def find_previous_palindrome_time(h, m):
while True:
m -= 1
if m < 0:
m = 59
h -= 1
if h < 0:
h = 23
if is_palindrome_time(f"{h:02}:{m:02}"):
return format_time(h, m)

def find_next_palindrome_time(h, m):
while True:
m += 1
if m == 60:
m = 0
h += 1
if h == 24:
h = 0
if is_palindrome_time(f"{h:02}:{m:02}"):
return format_time(h, m)

def main():
s = input().strip()
h, m = map(int, s.split(":"))

previous_palindrome = find_previous_palindrome_time(h, m)
next_palindrome = find_next_palindrome_time(h, m)

print(previous_palindrome)
print(next_palindrome)

if __name__ == "__main__":
main()

6. 几何计算:垂足坐标

问题描述

计算点 (x, y) 到线段 (a1, b1)(a2, b2) 的垂足坐标。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def perpendicular_foot(x, y, a1, b1, a2, b2):
dx, dy = a2 - a1, b2 - b1
t = (dx * (x - a1) + dy * (y - b1)) / (dx**2 + dy**2)
foot_x = a1 + t * dx
foot_y = b1 + t * dy
return foot_x, foot_y

def main():
x, y = map(float, input().strip().split())
a1, b1, a2, b2 = map(float, input().strip().split())
foot_x, foot_y = perpendicular_foot(x, y, a1, b1, a2, b2)
print(f"{foot_x:.6f} {foot_y:.6f}")

if __name__ == "__main__":
main()

7. 冒泡排序轮数

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def min_bubble_sort_rounds(num):
n = len(num)
rounds = 0
for i in range(n):
swapped = False
for j in range(n - 1 - i):
if num[j] > num[j + 1]:
num[j], num[j + 1] = num[j + 1], num[j]
swapped = True
if swapped:
rounds += 1
else:
break
return rounds

def main():
n = int(input().strip())
num = list(map(int, input().strip().split()))
result = min_bubble_sort_rounds(num)
print(result)

if __name__ == "__main__":
main()

8. 字典序最大子串

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def max_lexicographical_substring(s):
max_substring = ""
for i in range(len(s)):
if s[i:] > max_substring:
max_substring = s[i:]
return max_substring

def main():
s = input().strip()
result = max_lexicographical_substring(s)
print(result)

if __name__ == "__main__":
main()

9. 最小运载能力

问题描述

计算在给定天数内完成货物运输所需的最小运载能力。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def can_ship_with_capacity(weights, days, capacity):
current_weight = 0
days_needed = 1
for weight in weights:
if current_weight + weight > capacity:
days_needed += 1
current_weight = weight
if days_needed > days:
return False
else:
current_weight += weight
return True

def find_min_ship_capacity(weights, days):
left = max(weights)
right = sum(weights)
while left < right:
mid = (left + right) // 2
if can_ship_with_capacity(weights, days, mid):
right = mid
else:
left = mid + 1
return left

def main():
weights_size = int(input().strip())
weights = list(map(int, input().strip().split()))
days = int(input().strip())
result = find_min_ship_capacity(weights, days)
print(result)

if __name__ == "__main__":
main()

10. 石子游戏的最大得分

问题描述

在石子游戏中,通过合并石子堆使得得分最大化,得分由合并后的石子堆大小决定。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import heapq

def max_score(n, stones):
max_heap = [-stone for stone in stones]
heapq.heapify(max_heap)
total_score = 0
while len(max_heap) > 1:
x = -heapq.heappop(max_heap)
y = -heapq.heappop(max_heap)
score = (x + 1) * (y + 1)
total_score += score
heapq.heappush(max_heap, -(x + y))
return total_score

def main():
n = int(input().strip())
stones = [int(input().strip()) for _ in range(n)]
result = max_score(n, stones)
print(result)

if __name__ == "__main__":
main()