[백준] 벌집 nodejs

DongDong·2024년 8월 3일
0

난 이제 지쳤어요 땡벌 떙벌🐝

🔗 문제 링크

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

✅ 풀이

const INPUT = require("fs")
  .readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
  .toString()
  .trim()
  .split(" ");

function solution() {
  const input = Number(INPUT);
  const seq = 6;
  const arr = [1];
  let idx = 1;

  while (true) {
    const curValue = arr[arr.length - 1];
    if (input > curValue) {
      arr.push(curValue + seq * idx++);
    } else {
      break;
    }
  }
  console.log(arr.length);
}

solution();

후기

1,6,12,18 등 6n 만큼 증가하니까

1 -> 1
1~6 -> 7
1~6~12 -> 19
1~6~12~18 -> 37

이런식으로 찾고자 하는 숫자가 해당 범위 안에 있다면 array의 length가 1부터의 N까지 거리를 의미한다.
따라서 13은 7과 19 사이에 있으므로 3이 되는 것이다 :)

profile
중요한건 꺾이지 않는 마음

1개의 댓글

comment-user-thumbnail
2024년 10월 10일

:)

답글 달기

관련 채용 정보