728x90

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

Code

최종 제출 코드

😢 0!은 1을 잊고 있었다. 0 팩토리얼은 1이다.

😊 간단한 재귀 문제다. 가장 중요한 건 0!을 처리해주는 것이다.
피보나치수열 재귀와 비슷하다. n이 1로 갈때까지 재귀를 실행해주고 return하며 1부터 input까지 값을 곱해간다.

Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)

// Factorial
 
// For submit
 
// const fs = require('fs');
// const input = parseInt(fs.readFileSync('/dev/stdin').toString().trim());
 
// For local test
const input = 0; // 10
 
function factorial(n) {
if (n === 0) {
// 0! === 1
return 1;
}
 
if (n < 2) {
return n;
}
 
return factorial(n - 1) * n;
}
 
console.log(factorial(input));

+ Recent posts