题解 | #坐标移动#

坐标移动

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

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

void (async function () {
    // Write your code here
    const muban1 = /^[AWSD]\d$/;
    const muban2 = /^[AWSD]\d\d$/; // 加^和$达到完全匹配

    while ((line = await readline())) {
        let tokens = line.split(";");
        let x = 0;
        let y = 0;
        for (let i = 0; i < tokens.length; i++) {
            if (muban2.test(tokens[i]) || muban1.test(tokens[i])) {
                let direction = tokens[i][0];
                let number = parseInt(tokens[i].slice(1));
                switch (direction) {
                    case "A":
                        x -= number;
                        break;
                    case "D":
                        x += number;
                        break;
                    case "W":
                        y += number;
                        break;
                    case "S":
                        y -= number;
                        break;
                }
            }
        }
        console.log(`${x},${y}`);
    }
})();

全部评论

相关推荐

双非一本失业第二年:《机器视觉垃圾分类》
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务