题解 | #坐标移动#

坐标移动

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 str = await readline()
    const directionMap = {
        A: [-1, 0],
        D: [1, 0],
        W: [0, 1],
        S: [0, -1]
    }
    const point = [0, 0]
    const arr = str.split(';').forEach(item => {
        if (!item || item.length < 2 || item.length > 3) return
        const direction = item.slice(0, 1)
        let distance = item.slice(1)
        movePoint(point, direction, distance)
    })
    console.log(point.join(','))
	
    function movePoint(point, direction, distance) {
        if (!directionMap[direction] || isNaN(distance)) return
        const directionArr = directionMap[direction]
        point[0] = point[0] + directionArr[0] * distance
        point[1] = point[1] + directionArr[1] * distance
    }
}()

全部评论

相关推荐

我见java多妩媚:大外包
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务