题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

将输入值以分号隔开存放,对每一项判断是否为合法坐标,若合法,则按照字母和移动方向的关系更新x轴,y轴的值。

import sys
line = sys.stdin.readline().strip()
strList = line.split(';')
x, y = 0, 0
coord = set(['W', 'S', 'A', 'D'])
for item in strList:
    if  1 < len(item) < 4 and (item[0]  in coord) and item[1:].isdigit():       
        if item[0] == 'W':
            y += int(item[1:])
        if item[0] == 'S':
            y -= int(item[1:])
        if item[0] == 'A':
            x -= int(item[1:])
        if item[0] == 'D':
            x += int(item[1:])
print(str(x) + ',' +str(y))

#华为笔试题##算法备考#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务