57 lines
1.1 KiB
Plaintext
57 lines
1.1 KiB
Plaintext
/*
|
|
file name: fibUsingRecursive.c
|
|
description: Fibonacci numbers
|
|
利用函數遞迴呼叫做費氏數列計算
|
|
|
|
費氏數列為0,1,1,2,3,5,8,12,21,…
|
|
其中某一項為前二項之和,且第0項為0,第1項為1
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
|
|
/* 函數原型宣告 */
|
|
long Fibonacci(long);
|
|
void flushBuffer(void);
|
|
|
|
int main()
|
|
{
|
|
char ch;
|
|
long n;
|
|
|
|
printf("-----Fibonacii numbers Using Recursive-----");
|
|
do {
|
|
printf("\nEnter a number(n >= 0): ");
|
|
scanf("%ld", &n);
|
|
flushBuffer();
|
|
/* n 值大於0 */
|
|
if (n < 0)
|
|
printf("Number must be > 0\n");
|
|
else
|
|
printf("Fibonacci(%ld) = %ld\n", n, Fibonacci(n));
|
|
printf("Contiune(y/n)? ");
|
|
ch = toupper(getchar());
|
|
} while (ch == 'Y');
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* 利用遞迴函數呼叫本身函數來計算Fibonacci numbers */
|
|
long Fibonacci(long n)
|
|
{
|
|
if (n == 0) /* 第 0 項為 0 */
|
|
return 0;
|
|
else if (n == 1) /* 第 1 項為 1 */
|
|
return 1;
|
|
else /* 遞迴呼叫函數第 N 項為 n-1 跟 n-2 項之和 */
|
|
return(Fibonacci(n-1) + Fibonacci(n-2));
|
|
}
|
|
|
|
void flushBuffer()
|
|
{
|
|
while (getchar() != '\n') {
|
|
continue;
|
|
}
|
|
}
|