본문 바로가기

백준/JavaScript

[JS] 백준 일반 수학 1 - 다섯 번째 이야기

728x90

 

 

 

오늘은 자취방을 뺐다.

너무 힘들어...

 

 

문제 2292번

https://www.acmicpc.net/problem/2292

 

2292번: 벌집

위의 그림과 같이 육각형으로 이루어진 벌집이 있다. 그림에서 보는 바와 같이 중앙의 방 1부터 시작해서 이웃하는 방에 돌아가면서 1씩 증가하는 번호를 주소로 매길 수 있다. 숫자 N이 주어졌

www.acmicpc.net

 

이 문제는 범위가 증가할 때마다 둘러싼 방이 각 6의 배수로 증가하고 있다.

그래서 sum이 input보다 작을 때까지 6의 배수를 sum에 더해줬다. 

그리고 몇 번째로 더해줬는지 num을 출력해주면 된다.

 

const readFileSyncAdress = process.platform === 'linux' ? '/dev/stdin':'./input.txt'
const input = require("fs").readFileSync(readFileSyncAdress).toString().trim();
let num = 1;
let sum = 1;
while(sum<input){
    sum += 6*num;
    num++;
}
console.log(num);

 

 

 

[참고] https://velog.io/@dragoocho/%EB%B0%B1%EC%A4%80-2292-%EB%B2%88-Node.js-%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4

 

728x90