题解 | #坐标移动#

坐标移动

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

s = input().split(';')

location = [0, 0]  # 起始坐标为(0, 0)

for i in range(len(s)):
    if len(s[i]) < 2: 
        continue
    elif s[i][0] not in ['A', 'D', 'W', 'S']:
        continue
    elif (s[i][0] in ['A', 'D', 'W', 'S']) and (s[i].count('A') + s[i].count('D') + s[i].count('W') + s[i].count('S')) == 1:
        if len(s[i]) - 1 == 1:
            num = int(s[i][1])
        else:
            num = s[i][1]
            for j in range(2, len(s[i])):
                num = num + s[i][j]
            num = int(num)
        
        if s[i][0] == 'A':
            location[0] = location[0] - num
        elif s[i][0] == 'D':
            location[0] = location[0] + num
        elif s[i][0] == 'W':
            location[1] = location[1] + num
        elif s[i][0] == 'S':
            location[1] = location[1] - num

print(str(location[0]) + ',' + str(location[1]))

全部评论

相关推荐

07-02 13:52
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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