题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
import sys
import re
if __name__ == '__main__':
# 匹配每次位移方向及值的格式
pattern = re.compile("^[ADWSadws][0-9]+$")
for line in sys.stdin:
x, y = 0, 0
# 所有位移列表
offsets = []
input_strs = line.rstrip().split(";")
for input_str in input_strs:
if pattern.match(input_str):
offsets.append(input_str)
for offset in offsets:
direction = offset[0] # 方向
val = int(offset[1:]) #值
if direction == 'A' or direction == 'a':
x -= val
elif direction == 'D' or direction == 'd':
x += val
elif direction == 'W' or direction == 'w':
y += val
elif direction == 'S' or direction == 's':
y -= val
print(f"{x},{y}")
查看15道真题和解析
