返回
Featured image of post 洛谷-算法1-1-模拟与高精度

洛谷-算法1-1-模拟与高精度

在这里写下你的题注

【算法1-1】模拟与高精度

## [NOIP 2003 普及组] 乒乓球 ```python res = "" while True: try: line = input() if 'E' not in line: res += line else: res += line[:line.index('E')] break except: break

def play(game): cur_w, cur_l = 0, 0 for char in res: cur_w += int(char==‘W’) cur_l += int(char==‘L’) if max(cur_w, cur_l) >= game and abs(cur_w - cur_l) >= 2: print(f"{cur_w}:{cur_l}") cur_w, cur_l = 0, 0 print(f"{cur_w}:{cur_l}") print("")

play(11) play(21)

 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
42
43
44

## [NOIP 2015 普及组] 扫雷游戏

```python
# 读取输入的行数 m 和列数 n
m, n = map(int, input().split())

# 初始化一个列表来存储网格数据
mine = []
for _ in range(m):
    # 读取每一行的数据
    mine.append(input())

# 定义八个可能的方向,用于检查相邻的单元格
dir = [
    [0, 1], [0, -1], [1, 0], [-1, 0],
    [1, 1], [1, -1], [-1, 1], [-1, -1]
]

# 定义一个函数来计算指定位置 (x, y) 周围的雷的数量
def count(x, y):
    cnt = 0
    # 遍历所有八个方向
    for d in dir:
        dx, dy = x + d[0], y + d[1]
        # 检查新坐标是否在网格范围内
        if dx < 0 or dy < 0 or dx == m or dy == n:
            continue
        # 检查相邻单元格是否是雷
        if mine[dx][dy] == '*':
            cnt += 1
    return cnt

# 遍历网格的每一个位置
for i in range(m):
    for j in range(n):
        # 如果当前位置是雷,直接输出 '*'
        if mine[i][j] == '*':
            print('*', end='')
        else:
            # 否则,输出周围雷的数量
            print(count(i, j), end='')
    # 每行结束后换行
    print()

[NOIP 2016 提高组] 玩具谜题

这题主要是注意格式以及方向,别弄错

 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
# 读取输入的行数 m 和列数 n
m, n = map(int, input().split())

# 初始化方向列表和任务列表
dir = []
job = []
for _ in range(m):
    d, j = input().split()
    dir.append(int(d))
    job.append(j)

# 初始化索引
idx = 0

# 处理 n 次移动
for _ in range(n):
    right, step = map(int, input().split())
    # 确定是否顺时针移动
    clockwise = (dir[idx] and right) or (not dir[idx] and not right)
    if clockwise:
        idx = (idx + m - step) % m
    else:
        idx = (idx + step) % m

# 输出最终位置的任务
print(job[idx])

A+B Problem(高精)

别忘了进位,别忘了指针移动

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 读取两个大整数作为字符串输入
a, b = input(), input()

# 初始化指针和变量
i, j, carry, res = len(a), len(b), 0, ""

# 当还有位需要处理或者有进位时,继续循环
while i or j or carry:
    # 获取当前位的数字,如果没有则认为是0
    carry += int(a[i-1]) if i else 0
    carry += int(b[j-1]) if j else 0
    
    # 计算当前位的数字和进位
    carry, digit = divmod(carry, 10)
    
    # 将当前位的数字添加到结果字符串的前面
    res = str(digit) + res
    
    # 移动指针到下一位
    i, j = max(0, i-1), max(0, j-1)

# 输出最终结果
print(res)

A*B Problem 题解

 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
# 从输入读取两个字符串,每个字符串表示一个大整数,并将每个字符转换为整数
a = [int(char) for char in input()]
b = [int(char) for char in input()]

# 反转这两个列表,以便从最低位开始处理,并初始化一个结果列表,长度为两个列表长度之和,初始值为0
a, b, c = a[::-1], b[::-1], [0]*(len(a)+len(b))

# 遍历两个列表的每个元素,将其相乘,并将结果加到对应的位置
for i in range(len(a)):
    for j in range(len(b)):
        c[i+j] += a[i] * b[j]

# 处理进位,确保每个位置的值小于10
for i in range(len(c)-1):
    carry, c[i] = divmod(c[i], 10)  # 获取进位和当前位的值
    c[i+1] += carry  # 将进位加到下一位

# 去除前导零
for i in range(len(c)-1, -1, -1):
    if i and c[i] == 0:  # 如果是零且不是最低位
        c.pop()  # 移除该位

# 反转列表以得到正确的数字顺序,并将每个数字转换为字符串
c = c[::-1]
c = [str(num) for num in c]

# 将列表合并为字符串并打印
print(''.join(c))

[NOIP 1998 普及组] 阶乘之和

1
2
3
4
5
6
7
8
n = int(input())
res = 0
for i in range(n, 0, -1):
    tmp = 1
    for j in range(i, 0, -1):
        tmp *= j 
    res += tmp 
print(res)

[NOIP 2014 提高组] 生活大爆炸石头剪刀布

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def judge(x, y):
    if x == y: return 0 
    elif x > y: return -judge(y, x)
    elif x == 0 and (y == 1 or y == 4): return -1 
    elif x == 1 and (y == 2 or y == 4): return -1 
    elif x == 2 and y == 3: return -1 
    else: return 1 

n, na, nb = map(int, input().split())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))

pa, pb = 0, 0
for i in range(n):
    x, y = la[i%na], lb[i%nb]
    game = judge(x, y)
    if game == 1: pa += 1
    if game == -1: pb += 1 

print(f"{pa} {pb}")

[USACO2.4] 两只塔姆沃斯牛

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 读取10x10的网格
grid = [input().strip() for _ in range(10)]

# 初始化牛和农夫的位置
cow_position = [0, 0]
farmer_position = [0, 0]

# 初始化牛和农夫的方向(0: 上,1: 右,2: 下,3: 左)
cow_direction = 0
farmer_direction = 0

# 移动方向向量
directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]

# 查找牛和农夫的初始位置
for i in range(10):
    for j in range(10):
        if grid[i][j] == 'C':
            cow_position = [i, j]
        if grid[i][j] == 'F':
            farmer_position = [i, j]

# 模拟移动,最多1000步
for step in range(1, 1001):
    # 计算牛的下一个位置
    next_cow_position = [
        cow_position[0] + directions[cow_direction][0],
        cow_position[1] + directions[cow_direction][1]
    ]
    
    # 检查牛的下一个位置是否合法
    if (
        next_cow_position[0] < 0 or next_cow_position[0] == 10 or
        next_cow_position[1] < 0 or next_cow_position[1] == 10 or
        grid[next_cow_position[0]][next_cow_position[1]] == '*'
    ):
        # 如果不合法,改变方向
        cow_direction = (cow_direction + 1) % 4
    else:
        # 如果合法,移动到下一个位置
        cow_position = next_cow_position

    # 计算农夫的下一个位置
    next_farmer_position = [
        farmer_position[0] + directions[farmer_direction][0],
        farmer_position[1] + directions[farmer_direction][1]
    ]
    
    # 检查农夫的下一个位置是否合法
    if (
        next_farmer_position[0] < 0 or next_farmer_position[0] == 10 or
        next_farmer_position[1] < 0 or next_farmer_position[1] == 10 or
        grid[next_farmer_position[0]][next_farmer_position[1]] == '*'
    ):
        # 如果不合法,改变方向
        farmer_direction = (farmer_direction + 1) % 4
    else:
        # 如果合法,移动到下一个位置
        farmer_position = next_farmer_position

    # 检查是否相遇
    if cow_position == farmer_position:
        print(step)
        exit()  # 或者 raise SystemExit

# 如果1000步内没有相遇,输出0
print(0)

[NOIP 2009 普及组] 多项式输出

这题也太坑了,我觉得核心的方法如下:

  1. 处理符号:如果当前系数是正数且不是表达式的开头,则添加一个加号。
  2. 处理系数
    • 如果系数不为零,则将其转换为字符串并添加到结果中。
    • 如果系数是1或-1,并且不是常数项,则省略系数1。
  3. 处理变量部分
    • 对于非常数项,添加变量’x’。
    • 对于最高次项以外的项,添加指数部分。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
n = int(input())  # 读取用户输入的整数n,表示多项式的阶数
arr = list(map(int, input().split()))  # 读取用户输入的系数列表,转换为整数列表
res = ""  # 初始化结果字符串

for i in range(n + 1):  # 遍历从0到n的指数
    if res and arr[i] > 0:  # 如果结果字符串不为空且当前系数为正数,添加加号
        res += '+'
    if arr[i]:  # 如果当前系数不为零
        res += str(arr[i])  # 添加系数到结果字符串
        if i < n and abs(arr[i]) == 1:  # 如果指数小于n且系数绝对值为1,去掉系数1
            res = res[:-1]
        if i < n:  # 如果是最后一位(线性项),添加'x'
            res += 'x'
        if i < n - 1:  # 如果不是常数项或线性项,添加'x^指数'
            res += f"^{n - i}"
print(res)  # 打印最终的结果字符串

[NOIP 2007 提高组] 字符串的展开

这里的问题是分段可能为空的情况,也就是前缀-和后缀-的情况,这个卡住了

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 输入三个参数 p1, p2, p3,分别表示填充模式、填充长度和填充顺序
p1, p2, p3 = map(int, input().split())

# 将输入字符串按 '-' 分割成段,存储在列表 seg 中
seg = list(input().split('-'))

# 初始化结果字符串 res
res = ""

# 定义函数判断字符是否为数字
def is_num(x):
    return ord(x) >= ord('0') and ord(x) <= ord('9')

# 定义函数判断字符是否为小写字母
def is_char(x):
    return ord(x) >= ord('a') and ord(x) <= ord('z')

# 遍历 seg 列表,处理每一段
for i in range(len(seg)):
    # 将当前段添加到结果字符串 res 中
    res += seg[i]
    
    # 如果是最后一段,则结束循环
    if i + 1 == len(seg):
        break 
    
    # 如果当前段或下一段为空字符串,则直接添加 '-' 并跳过后续处理
    if not len(seg[i]) or not len(seg[i+1]):
        res += '-'
        continue
    
    # 获取当前段的最后一个字符和下一段的第一个字符
    l, r = seg[i][-1], seg[i+1][0]
    
    # 如果 l 的 ASCII 值大于或等于 r 的 ASCII 值,则直接添加 '-'
    if ord(l) >= ord(r):
        res += '-'
    
    # 如果 l 和 r 都是数字,或者都是小写字母,则进行填充操作
    elif (is_num(l) and is_num(r)) or (is_char(l) and is_char(r)):
        tmp = ""
        
        # 生成填充字符
        for j in range(ord(l)+1, ord(r)):  # 注意:变量名从 i 改为 j,避免冲突
            if p1 == 1 or (p1 == 2 and is_num(l)):  # 模式1或模式2(数字)
                tmp += chr(j) * p2 
            if p1 == 2 and is_char(l):  # 模式2(字母),转换为大写
                tmp += chr(j - ord('a') + ord('A')) * p2 
            if p1 == 3:  # 模式3,用 '*' 填充
                tmp += '*' * p2 
        
        # 如果 p3 == 2,则将填充字符逆序
        if p3 == 2:
            tmp = tmp[::-1]
        
        # 将填充字符添加到结果字符串 res 中
        res += tmp 
    else:
        # 如果 l 和 r 不满足条件,则直接添加 '-'
        res += '-'

# 输出最终结果
print(res)

后面不想写了

Licensed under CC BY-NC-SA 4.0
© 2023 - 2025 壹壹贰捌· 0Days
共书写了257k字·共 96篇文章 京ICP备2023035941号-1