题解 | #坐标移动#

坐标移动

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
    }

}

全部评论

相关推荐

会考什么呀?硬件岗
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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