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
'코테 > 코테withJS' 카테고리의 다른 글
| 백준 Node.js(자바스크립트) 1267번 핸드폰 요금 (0) | 2021.09.21 |
|---|---|
| 백준 Node.js(자바스크립트) 2446문제 별찍기 -9 (0) | 2021.09.20 |
| 백준 Node.js(자바스크립트) 2839번 설탕배달 (0) | 2021.09.10 |
| 백준 Node.js(자바스크립트) 2869번 달팽이올라가는 문제 (0) | 2021.08.29 |
| 백준 Node.js(자바스크립트) 1712번 손익분기점 문제 (0) | 2021.08.20 |