题解 | #坐标移动#

坐标移动

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
    while ((line = await readline())) {
        let dictions = line.split(";");
        let x = 0; // 横向
        let y = 0; // 纵向
        let isDiction = /^[A|D|W|S][0-9]{1,2}$/;// 用来检测格式是否正确
        dictions.forEach((item) => {
            let i = 0;
            if (isDiction.test(item)) {
                let d = item.slice(0, 1);
                let count = parseInt(item.slice(1));
                switch (d) {
                    case "A":
                        x -= count;
                        break;
                    case "D":
                        x += count;
                        break;
                    case "W":
                        y += count;
                        break;
                    case "S":
                        y -= count;
                        break;
                    default:
                        break;
                }
            }
        });
        console.log(`${x},${y}`);
    }
})();

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 18:03
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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