题解 | #坐标移动#

坐标移动

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 x = 0,
            y = 0;
        arr = line.split(";");
        let reg = /^[A|S|W|D]\d{0,2}$/;
        for (let k of arr) {
            if (!reg.test(k)) {
                continue;
            } else {
                let arr1 = k.split("");
                let direction = arr1[0];
                arr1.shift();
                let distance = Number(arr1.join(''));
                switch (direction) {
                    case "A":
                        x -= distance;
                        break;
                    case "D":
                        x += distance;
                        break;
                    case "W":
                        y += distance;
                        break;
                    case "S":
                        y -= distance;
                        break;
                    default:
                        break;
                }
            }
        }
        let str1 = x + "," + y;
        console.log(str1);
    }
})();

全部评论

相关推荐

07-07 11:33
江南大学 Java
已经在暑假实习了 ,没有明确说有hc,纠结实习到八月份会不会有点影响秋招毕竟感觉今年好多提前批
程序员小白条:92的话准备提前批,其他没必要,没面试机会的,而且你要准备充分,尤其八股和算法题
点赞 评论 收藏
分享
这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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