algorithm/BOJ

BOJ 10872번 팩토리얼

_JunHo 2020. 1. 13. 18:56

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 == 0cout << "1";
    else cout << factorial(N);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter