题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
# 这题比较麻烦的是处理输入 # 输入一行字符串,首先根据';'做切割, split # 逐个判断每个一个元素是否有效:一个字母+2个以内的数字组成。 # 若有效,根据字母分别在坐标上进行不同的移动操作 # 输出坐标 s = input().split(';') pos0 = [0,0] direction = ['A','D','W','S'] num = [[-1,0],[1,0],[0,1],[0,-1]] for each in s: if len(each)>0 and len(each)<4: # 判断第一个字母的有效性 flag1 = each[0] in direction # 判断2和3是否都是数字 flag2 = each[1:].isdigit() if flag1 and flag2: index = direction.index(each[0]) for i in range(2): pos0[i] += num[index][i] * int(each[1:]) print(','.join(map(str,pos0)))