题解 | #坐标移动#

坐标移动

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

words = input().strip().split(';')
xy = [0,0]#初始坐标

def getCoordinate(word):#分析坐标是否合法
    if len(word)>=2:
        direction = word[0]
        if direction not in {'A','D','W','S'}:
            return 0,0 # 方向不合法
        num = word[1:]
        try:
            num = int(num)
        except ValueError:
            return 0,0 #数字不合法
        return direction,num
        
    return 0,0 #长度不合法
for word in words:
    #判断坐标是否合法
    direction,num = getCoordinate(word) #获取坐标及方向
    if direction:#
        if direction == 'A':
            xy[0]-=num
        elif direction == 'D':
            xy[0]+=num
        elif direction == 'W':
            xy[1]+=num
        elif direction == 'S':
            xy[1]-=num
print(str(xy[0])+','+str(xy[1]))
全部评论

相关推荐

01-04 21:30
已编辑
河南工业大学 Java
27届学院本誓死冲击...:下次再发把个人信息隐藏掉,以防有心之人。相关课程删了,荣誉奖项只留蓝桥杯,把蓝桥杯写到教育经历里,按教育经历、实习经历、项目经历、专业技能这个顺序排版
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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