BOJ : https://www.acmicpc.net/problem/10872
GitHub : https://github.com/junho0956/Algorithm/blob/master/10872/10872/%EC%86%8C%EC%8A%A4.cpp
팩토리얼 큰 수가 될수록 반복계산에 의해서 시간이 오래걸리므로 처리해주자
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <iostream>
using namespace std;
long long dp[13];
long long factorial(long long n) {
if (dp[n]) return dp[n];
if (n == 1) {
dp[n] = 1;
return dp[n];
}
return n * factorial(n - 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N;
cin >> N;
if (N == 0) cout << "1";
else cout << factorial(N);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 2004번 조합 0의 개수 (0) | 2020.01.13 |
---|---|
BOJ 1676번 팩토리얼 0의 개수 (0) | 2020.01.13 |
BOJ 11653번 소인수분해 (0) | 2020.01.13 |
BOJ 6588번 골드바흐의 추측 (0) | 2020.01.13 |
BOJ 1929번 소수 구하기 (0) | 2020.01.13 |