题解 | #坐标移动#

坐标移动

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

let directives = readline().split(';');
// 指令校验表达式
const reg = /^[ADWS](?:[0-9]|[1-9][0-9]{1})$/;
// 保留有效的指令
directives = directives.filter(d => {
  return reg.test(d);
});
// 坐标[x, y]
const coord = [0, 0];
function directExecutor(d) {
  const reg = /^([ADWS])([0-9]|[1-9][0-9]{1})$/;
  const [, direction, step] = reg.exec(d);
  switch(direction) {
    case 'A':
      coord[0] -= Number.parseInt(step, 10);
      break;
    case 'D':
      coord[0] += Number.parseInt(step, 10);
      break;
    case 'W':
      coord[1] += Number.parseInt(step, 10);
      break;
    case 'S':
      coord[1] -= Number.parseInt(step, 10);
      break;
  }
}
directives.forEach(d => {
  directExecutor(d);
});

console.log(`${coord[0]},${coord[1]}`);

全部评论

相关推荐

昨天 11:15
中南大学 Java
好可爱的hr姐姐哈哈哈哈
黑皮白袜臭脚体育生:兄弟们貂蝉在一起,吕布开了
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
练习生懒羊羊:开飞机把这个公司创飞吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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