题解 | #坐标移动#

坐标移动

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: string) {
    let tokens = line.split(";");
    let ret = [0, 0];
    let f = /^[WSAD]\d{1,2}$/;
    tokens.forEach((v) => {
        f.lastIndex = 0;
        if (f.test(v)) {
            switch (v.charAt(0)) {
                case "W": {
                    ret[1] += parseInt(v.slice(1));
                    break
                }
                case "S": {
                    ret[1] -= parseInt(v.slice(1));
                    break
                }
                case "A": {
                    ret[0] -= parseInt(v.slice(1));
                    break
                }
                case "D": {
                    ret[0] += parseInt(v.slice(1));
                    break
                }
            }
        }
    });
    console.log(ret.toString())
});

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务