题解 | #坐标移动#

坐标移动

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 arr = line.split(';');
        let x = 0;
        let y = 0;
        for(let i = 0; i < arr.length; i++) {
			// 判断有效值
            if(isValidVal(arr[i])) {
				// 匹配该项值,match[1] 是方向,match[2] 是步数
                let match = arr[i].match(/(^[ADWS])(\d+$)/);
                switch(match[1]){
                    case "D": x += parseInt(match[2]);break;
                    case "A": x -= parseInt(match[2]);break;
                    case "W": y += parseInt(match[2]);break;
                    case "S": y -= parseInt(match[2]);break;
                }
            }
        }
        console.log(`${x},${y}`);
    }
}()

var isValidVal = function(val) {
    return /^[ADWS]\d+$/.test(val);
}

var moveTo = function() {}

全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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