题解 | #坐标移动#

坐标移动

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

    const input = readline();
    if (input) {
        const o_arr = input.split(';');
        const direction = ['A', 'S', 'D', 'W'];
        const o = {
            x: 0,
            y: 0,
        }; // 原点
        o_arr.forEach(item => {
            const sub1 = item.substr(0, 1);
            const sub2 = item.substr(1, item.length);
            const isNumber = typeof(Number(sub2)) === 'number' && !isNaN(Number(sub2));
            if (direction.includes(sub1) && isNumber) {
                switch (sub1) {
                    case 'A': o.x -= Number(sub2);
                        break;
                    case 'S': o.y -= Number(sub2);
                        break;
                    case 'D': o.x += Number(sub2);
                        break;
                    case 'W': o.y += Number(sub2);
                        break;
                    default:
                        break;
                }
            }
        })
        print(`${o.x},${o.y}`);
    } else {
        break;
    }

}
全部评论

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务