알고리즘/백준

2747>피보나치

Diademata 2017. 12. 2. 21:08
반응형

https://www.acmicpc.net/problem/2747


문제

피보나치 수는 0과 1로 시작한다. 0번째 피보나치 수는 0이고, 1번째 피보나치 수는 1이다. 그 다음 2번째 부터는 바로 앞 두 피보나치 수의 합이 된다.


이를 식으로 써보면 Fn = Fn-1 + Fn-2 (n>=2)가 된다.


n=17일때 까지 피보나치 수를 써보면 다음과 같다.


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597


n이 주어졌을 때, n번째 피보나치 수를 구하는 프로그램을 작성하시오.


입력

첫째 줄에 n이 주어진다. n은 45보다 작거나 같은 자연수이다.


code>>


#include<stdio.h>

#include <string.h>

long long memo[10000];

long long fibo(int input)

{

if (input ==0 || input == 1) return input;

if (memo[input] != -1) return memo[input];

return memo[input] = fibo(input - 1) + fibo(input - 2);

}

int main()

{

memset(memo, -1, sizeof(memo));

int input = 10;

scanf("%d", &input);

printf("%lld", fibo(input));

    return 0;

}

반응형