题解 | #坐标移动#

坐标移动

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let str:string[]

rl.on('line', function (line) {
    str = line.split(';');
});
rl.on('close',() => {
    const reg = new RegExp(/^[ADWS][0-9]{1,2}$/)
    let X:number = 0
    let Y:number = 0
    str = str.filter( i => {
        return reg.test(i)
    })
    str.forEach( i => {
        let direction = i.slice(0,1)
        let step = i.slice(1)
        if(direction == 'A') X = X - parseInt(step)
        if(direction == 'S') Y = Y - parseInt(step)
        if(direction == 'W') Y = Y + parseInt(step)
        if(direction == 'D') X = X + parseInt(step)
    })

    console.log(X+','+Y)
})

全部评论

相关推荐

你背过凌晨4点的八股文么:简历挂了的话会是流程终止,像我一样
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务