Skip to content
On this page

백준 2231 JavaScript

On this page

문제

백준 2231

풀이

완전 탐색 알고리즘으로 푼다. 어떤 자연수 N의 분해합은 N보다 크다. 그러니 N의 생성자는 N보다 작을 것.

N < 분해합
245 < 245 + 2 + 4 + 5 (256)

여기서 256의 생성자는 245이니 생성자는 N보다 작다. 따라서 N까지 수의 분해합을 반복문을 통해 계속 계산해서 처음으로 분해합이 N일 때 반복문을 멈추면 된다. N까지 수 중에 나오지 않으면 0을 출력한다.

const readFileSyncPath = '/dev/stdin';
// 자연수 N
const N = Number(require('fs').readFileSync(readFileSyncPath).toString().trim())
// 256 (= 245 + 2 + 4 + 5)
// 245는 256의 생성자
// 자연수 N의 생성자 구하기
let constructor = 0
for (let i = 1; i <= N; i++) {
let sum = i
.toString()
.split('')
.map(Number)
.reduce((prev, curr) => {
return prev + curr
}, i)
if (sum === N) {
constructor = i
break
}
}
console.log(constructor)
Edit this page
Last updated on 8/13/2022