题解 | #HJ17 坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import re
def calculate_final_position(moves):
# 初始坐标
x, y = 0, 0
# 方向映射
direction_map = {'A': (-1, 0), 'D': (1, 0), 'W': (0, 1), 'S': (0, -1)}
# 正则表达式匹配合法坐标
pattern = re.compile(r'^([ADWS])(\d{1,2})$')
for move in moves.split(';'):
match = pattern.match(move)
if match:
direction, step = match.groups()
dx, dy = direction_map[direction]
x += dx * int(step)
y += dy * int(step)
return x, y
# 输入
input_string = input()
# 计算最终坐标
final_position = calculate_final_position(input_string)
# 输出结果
print(f"{final_position[0]},{final_position[1]}")
查看11道真题和解析