Created
April 4, 2011 19:28
-
-
Save agiletalk/902244 to your computer and use it in GitHub Desktop.
재귀 함수를 이용한 피보나치 수열
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int fibonacci(int); | |
int main() | |
{ | |
int i, n; | |
printf("n: "); | |
scanf("%d", &n); | |
for(i = 0; i < n; i++) | |
{ | |
printf("%d ", fibonacci(i)); | |
} | |
return 0; | |
} | |
int fibonacci(int n) | |
{ | |
// F(0) = 0 and F(1) = 1 | |
// 재귀 함수가 끝나는 종료조건 | |
if(n == 0 || n == 1) | |
return n; | |
// F(n) = F(n-1) + F(n-2) | |
// 재귀 함수에서 재귀되는 부분 | |
else | |
return fibonacci(n-1) + fibonacci(n-2); | |
/* one line */ | |
// return (n == 0 || n == 1) ? n : fibonacci(n-1) + fibonacci(n-2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment