题解 | #坐标移动#

坐标移动

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

1、利用正则表达式筛选出符合条件的数据 2、分割字符串判断并运行 具体代码如下:

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    while ((line = await readline())) {
        let a = 0;
        let b = 0;
        let tokens = line.split(";");
        for (let i = 0; i < tokens.length; i++) {
            if (tokens[i].match(/^[WASD]\d+$/i)) {
                switch (tokens[i][0]) {
                    case 'W':
                        b += parseInt(tokens[i].substring(1));
                        break;
                    case 'A':
                        a -= parseInt(tokens[i].substring(1));
                        break;
                    case 'S':
                        b -= parseInt(tokens[i].substring(1));
                        break;
                    case 'D':
                        a += parseInt(tokens[i].substring(1));
                        break;
                    default:
                        break;
                }
            }
        }
        console.log(`${a},${b}`);
    }
})();

#0offer是寒冬太冷还是我太菜#
全部评论

相关推荐

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