题解 | #坐标移动#

坐标移动

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

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
rl.on('line', function (line) {
    const tokens = line.split(';');

    // 定义零点坐标 x,y
    const point = [0,0]
    // 定义位移
    const move = {
        A: -1,
        S: -1,
        D: 1,
        W: 1
    }

    for(let i = 0; i < tokens.length; i++){
        // 不符合要求的就跳过
        if(!/^[ADWS][0-9]{1,2}$/.test(tokens[i])) continue
        if(tokens[i].includes('A') || tokens[i].includes('D'))  point[0] += move[tokens[i].slice(0,1)] * tokens[i].slice(1)
        if(tokens[i].includes('W') || tokens[i].includes('S')) point[1] += move[tokens[i].slice(0,1)] * tokens[i].slice(1)
    }

    console.log(point.join(','))
});

全部评论

相关推荐

2025-12-01 15:04
吉首大学 后端工程师
冲鸭2024:亚信不去也罢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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