#include <stdio.h> int Fibonacci (int n); int main (void) { int n; printf("please enter a number(q to quit): "); while ((scanf("%d",&n)) == 1) { Fibonacci(n); printf("please enter another number(q to quit): "); } return 0; } int Fibonacci (int n) { int i; int f_1, f_2, f_3; printf("the sequence of Fibonacci is: "); for (i = 1; i <= n; i++) { if (i ==1) { f_1 = 1; printf("%d ", f_1); } else if (i == 2) { f_2 = 1; printf("%d ",f_2); } else { f_3 = f_1 +f_2; f_1 = f_2; f_2 = f_3; printf("%d ",f_3); } } putchar('\n'); return; }
9.10 编写并测试Fibonacii函数,该函数用循环代替递归计算斐波那契数。
猜你喜欢
转载自blog.csdn.net/qq_36324796/article/details/78993870
今日推荐
周排行