斐波那契数列编程C语言

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36782456/article/details/72417876

斐波那契数列指的是这样一个数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

#include <stdio.h>
long long Fbnl(int N)   //非递归
{
    int  a1 = 0;
    int  a2 = 1;
    int  a3 = N;
    printf("  0  ");
    for (int i = 2; i <N; i++)
    {

        printf("%2d  ", a2);
        a3 = a2 + a1;
        a1= a2;
        a2 = a3;

    }
    printf("\n");
    return a3;
}
//                        递归
//long long Fbnl(int N)      
//{
//  return N < 2 ? N : Fbnl(N - 1) + Fbnl(N - 2);
//}
int main()
{
    int tmp = Fbnl(7);
    printf("%d\n", tmp);
    return 0;
}
展开阅读全文

没有更多推荐了,返回首页