본문 바로가기
코테/코테withJS

백준 Node.js(자바스크립트) 1011문제 Fly me to the Alpha Centauri

by 트레일헤드레인저 2021. 9. 10.
SMALL

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
let input = fs.readFileSync(filePath).toString().split('\n');
let testCase = input[0];

solution();

function solution() {
  for (let i = 1; i <= testCase; i++) {

    let [A, B] = input[i].split(" ");
    let distance = B - A;

    let rootD = Math.sqrt(distance);
  
    if(rootD == Math.floor(rootD)){

      console.log(rootD + (rootD-1));

    }else {
      console.log(Math.floor(rootD*2));
    }
  }
}

뭔말인지 몰라서 포기할까하다 다른 글에서 문제를 잘보고 하나씩 적어보면 패턴이 보인다고하여서 적어보고 발견

if(rootD == Math.floor(distance)

>> √n이 자연수일 경우에는 √n + √n-1을 하면 됨

 

else

>> √n이 딱 떨어지지않을경우 n의제곱 ~ (n+1)의 제곱 사이에 있는 건데 아무 수나 넣고 적어보니 √n를 2곱하고 소수점을 뺐다.

LIST