Algorithm

[백준] 알고리즘 10872번 - 팩토리얼 문제

낭강 2020. 12. 29. 18:44

#include <bits/stdc++.h>
using namespace std;
int factory(int n) {

	if (n==0||n==1) {
		return 1;
	}
	else {
		return factory(n - 1) * n;
	}
}
int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n,ans=0;
	cin >> n;
	ans = factory(n);
	cout << ans;
}
 

0! 과 1!의 예외만 처리해주면 쉽게 해결할 수 있다.

다만 n이 커질경우 int로는 정수를 다 담을 수 없게될 것이다.

그래서 문제에서도 n의 범위를 12까지만 준거같다.