题解 | 坐标移动

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {

    let operatorList = (await readline()).split(';');
    let operators = ['A','D','W','S'];
    let startPos = [0,0];
    let moveItem = '';
    let moveDirection = '';
    let moveNum = '';
    for(let i in operatorList){
        moveItem = operatorList[i];
        if(moveItem){
            moveDirection = moveItem.slice(0,1);
            if(operators.indexOf(moveDirection)>-1){
                moveNum = moveItem.slice(1);
                if(parseInt(moveNum) && moveNum<100 && moveNum>0){
                    moveNum = parseInt(moveNum);
                    if(moveDirection == 'A'){
                        startPos[0]-=moveNum;
                    }else if(moveDirection=='D'){
                        startPos[0]+= moveNum;
                    }else if(moveDirection=='W'){
                        startPos[1]+= moveNum;
                    }else if(moveDirection=='S'){
                        startPos[1]-= moveNum;
                    }
                }
            }
        }
    }
        console.log(startPos[0]+','+startPos[1]);
    
}()

全部评论

相关推荐

2024-12-12 11:34
门头沟学院 C++
百度 移动端开发 (n+2) * 16,3w签字费
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务