글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자를 적어주세요. 글의 요약 설명 부분. 150자입니다

 

목차

     

     


     

     

    [백준 24416번 C/C++] 알고리즘 수업 - 피보나치 수 1

     

     


     

    해결전략

     

     

    https://sectumsempra.tistory.com/86

     

     


     

    코드

     

    #include<iostream>
    using namespace std;
    
    int f[41];
    int n, cnt1, cnt2;
    
    int fib(int x)
    {	
    	if (x == 1 || x == 2 )
    	{
    		cnt1++;
    		return 1;
    	}
    	
    	return fib(x-1) + fib(x-2);
    }
    
    int fibonacci(int a)
    {
    	f[1]=1;
    	f[2]=1;
    	for(int i=3; i<=n; i++){
    		cnt2++;
    		f[i]=f[i-1]+f[i-2];
    	}
    	return f[a];
    }
    
    int main() {
    	freopen("input.txt", "rt", stdin);
    	scanf("%d", &n);
    	
    	fib(n);
    	fibonacci(n);
    		
    	printf("%d %d", cnt1, cnt2);
    }