题解 | 坐标移动

坐标移动

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

import sys
x,y = (0,0)
char_list = ['A','S','D','W']
for line in sys.stdin:
    a = line.split()
    # print(int(a[0]) + int(a[1]))
# print(a)
m_str = a[0].split(";")
# print(m_str)
for s in m_str:
    if len(s) < 2:
        # print(f"{s}长度不够2位")
        continue
    if len(s) > 3:
        # print(f"{s}长度超过3位")
        continue
    if s[0] not in char_list:
        # print(f"{s}第一个字符{s[0]}不在asdw中")
        continue
    if not '1' <= s[1] <= '9':
        # print(f"{s}第二个字符{s[1]}应该是1~9")
        continue
    if len(s) == 3:
        if not  '0' <= s[2] <= '9':
            # print(f"有第三个字符,{s}第三个字符{s[1]}应该0-9")
            continue
        else:
            num = int(s[1:])
            if s[0] == 'A':
                x = x - num
            elif s[0] == 'D':
                x = x + num
            elif s[0] == 'S':
                y = y - num
            elif s[0] == 'W':
                y = y + num 
    else:
        num = int(s[1])
        if s[0] == 'A':
            x = x - num
        elif s[0] == 'D':
            x = x + num
        elif s[0] == 'S':
            y = y - num
        elif s[0] == 'W':
            y = y + num
print(f"{x},{y}")

全部评论

相关推荐

青春运维少年不会梦到...:实习大王
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务