Deff_Dev
[코드업] (재귀함수) 팩토리얼 계산 (C++) 본문
(재귀함수) 팩토리얼 계산
팩토리얼(!)은 다음과 같이 정의된다. $n!=n\times(n-1)\times(n-2)\times\cdots \times2\times1$ 즉, $5! = 5 \times 4 \times 3 \times 2 \times 1 = 120$ 이다. $n$이 입력되면 $n!$의 값을 출력하시오. 이 문제는 반복문 for, while
codeup.kr
문제
이 문제는 입력된 정수 n의 팩토리얼 (!)의 값을 구하는 문제이다.
풀이
재귀 함수를 이용하여 풀이했다.
#include <iostream>
// https://codeup.kr/problem.php?id=1912&rid=0
using namespace std;
int Func(int n) {
if (n == 1) {
return 1;
}
// n이 5일 때 => 1 * 2 * 3 * 4 * 5
return Func(n - 1) * n;
}
int main() {
int n;
cin >> n;
cout << Func(n);
return 0;
}
CodingTestPractice/CodeUp/재귀 함수/(재귀함수) 팩토리얼 계산.cpp at main · seungdo1234/CodingTestPractice
코딩 테스트 연습. Contribute to seungdo1234/CodingTestPractice development by creating an account on GitHub.
github.com
'코딩테스트 > 코드업' 카테고리의 다른 글
[코드업] 디지털 도어 락 (C++) (0) | 2024.05.05 |
---|---|
[코드업] 나도 IQ 150 (C++) (2) | 2024.04.10 |
[코드업] (재귀함수) 2진수 변환 (C++) (0) | 2024.04.07 |
[코드업] 계단 오르기 2 (C++) (2) | 2024.04.06 |
[코드업] 기억력 테스트 2 (C++) (1) | 2024.04.05 |