题解 | #坐标移动#

坐标移动

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let index = 0
let input =[]
rl.on('line', function (line) {
    soluiton(line)
});

function soluiton(line:string){
    let moves = line.split(";")
    f(0,0,0,moves)
}
var pat =/^[AWSD][0-9]+$/
function f(x,y,index,moves){
    if(index>moves.length){
        console.log([x,y].join(","))
        return
    }
    let move = moves[index]
    if(!pat.test(move)){
        return f(x,y,index+1,moves)
    }
    if(move[0]==="A"){
        let distance =Number(move.slice(1))
        return f(x-distance,y,index+1,moves)
    }
    if(move[0]==="D"){
        let distance =Number(move.slice(1))
        return f(x+distance,y,index+1,moves)
    }
    if(move[0]==="W"){
        let distance =Number(move.slice(1))
        return f(x,y+distance,index+1,moves)
    }
    if(move[0]==="S"){
        let distance =Number(move.slice(1))
        return f(x,y-distance,index+1,moves)
    }
}

#递归#
全部评论

相关推荐

挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务