12.DynamicProgramming - C++

less than 1 minute read

12.DynamicProgramming - C++




#include <iostream>

using namespace std;
int f[100]={0};
 
int fib(int n){
    if(n==0) return 0;
    else if(n==1) return 1;
    else if(f[n]) return f[n]; // 메모리제이션
    else{
        f[n]=fib(n-1)+fib(n-2);
        return f[n];
    }
}
 
int main(){
    int N;
    cin>>N;
    cout<<fib(N)<<'\n';
    return 0;
}
 
// 5 입력시 55 결과값





Leave a comment