题解 | #坐标移动#

坐标移动

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

import Foundation


var x = 0
var y = 0

while let line = readLine() {
    
    let list = line.components(separatedBy: ";")

    for item in list {

        if item.count > 1 {

            if item.hasPrefix("A") {
                x -= getValue(item)
            } else if item.hasPrefix("D") {
                x += getValue(item)
            } else if item.hasPrefix("W") {
                y += getValue(item)
            } else if item.hasPrefix("S") {
                y -= getValue(item)
            } 
        }
        
    }
    
    print("\(x),\(y)")
     

}

func getValue(_ text: String) -> Int {

    let value1 = text.substring(from: 1)
    if let value2 = Int(value1) {
        return value2
    } else {
        return 0
    }

}

全部评论

相关推荐

08-05 14:06
门头沟学院 Java
驼瑞驰_招募评论官版...:哈哈哈,hr怪实诚的
点赞 评论 收藏
分享
07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
08-04 21:05
门头沟学院 C++
只会一面挂:是这样的,最多人卷的方向,招最少人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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