- 論壇徽章:
- 0
|
請各問各位前輩,用C語言編一個程序計算出斐波那契數列的第N位數,如果不用遞歸算法而是用循環(huán)的話,應該如何實現呢(最好也不用數組)?我想了一個下午都沒有任何進展,請各位幫忙看看。感激不盡!
下面我做了個函數調用的測試,但關鍵部位還是想不出來。- #include <stdio.h>
- unsigned long test_fibonacci(int n);
- int main(void)
- {
- int n;
-
- printf("Enter a positive integer (q to quit): ");
- while(scanf("%d", &n) == 1)
- {
- printf("\nThe %dth number in the fibonacci numbers is %lu",
- n, test_fibonacci(n));
- printf("\nEnter a positive integer (q to quit): ");
- }
- puts("\nDone!");
- return 0;
- }
- unsigned long test_fibonacci(int n)
- {
- int i, sum = 0;
- if(n <= 2)
- sum = 1;
- else
- for(i = 2; i <= n; i++)
- {
- }
- return sum;
- }
復制代碼 |
|