C语言求Fibonacci数列

方法有三种甚至更多,但核心思想都是一个数列的通项公式:F(n)=F(n-1)+F(n-2)。核心代码放在最后。

方法一:普通法

#include <stdio.h>
#include <stdlib.h>
unsigned long Fibonacci(unsigned n);/*打印Fibonacci*/

int main(void)
{
    unsigned int q;
    int count;

    printf("This program is using for Fibonacci.");
    printf("Please enter a number that you want to see the Fibonacci:");

    while (scanf("%u", &q) == 1)
    {
        for (count = 1; count <= q; count++)
            printf("%lu ", Fibonacci(count));
        printf("\n\aPlease enter the next number:");
        continue;
    }
    printf("\aThank you for you using.");

    return EXIT_SUCCESS;
}

unsigned long Fibonacci(unsigned n)
{
    int a = 1, b = 1;
    int i, temp;

    if (n > 2)
        for (i = 3; i <= n; i++)
        {
            temp = a + b; /*交换数值*/
            a = b;
            b = temp;
        }
    else
        b = 1;

    return b;
}

方法二:利用数组

#include <stdio.h>
#include <stdlib.h>
#define upper 57
unsigned int Fibonacci(unsigned int a); /*打印Fibonacci*/
unsigned int check(void);               /*检查输入是否为数字的错误*/

int main(void)
{
    unsigned int num; /*Fibonacci数列的项数*/
    printf("This program is using for Fibonacci array.\n");
    printf("Please enter the number that you want to see the "
           "Fibonacci(more than 0,less than 4294967295)"
           " (For your computer's speed of operation considerations,"
           "generally do not recommend more than 10000):");
    /*因为用的是unsigned int类型,所以建议输入大于0小于4294967295*/
    while (1) /*无条件进入循环*/
    {
        num = check();

        Fibonacci(num);
                    /*准备开始第n(n>1)次循环*/
        printf("This program is using for Fibonacci array.\n");
        printf("Please enter the number that you want to see "
               "the Fibonacci(less than 4294967295)"
               " (For your computer's speed of operation "
               "considerations, generally do not recommend "
               "more than 10000)");
    }
    printf("Done!");

    return EXIT_SUCCESS;
}

unsigned int Fibonacci(unsigned int a) /*Fibonacii数列求解运算*/
{
    unsigned int arr[a];
    arr[0] = 1;
    arr[1] = 1;
    if (a > 2) /*求出第3项及以后的结果*/
    {
        printf("%u %u", arr[0], arr[1]);
        for (int i = 2; i < a; i++)
        {
            arr[i] = arr[i - 1] + arr[i - 2];
            printf("  %u", arr[i]);
            if (i == (a - 1))
            {
                printf("\n");
            }
        }
    }
    else /*处理当输入1 or 2时的情况*/
    {
        if (a == 1)
        {
            printf("%u\n", arr[1]);
        }
        if (a == 2)
        {
            printf("%u %u\n", arr[1], arr[1]);
        }
    }
}

unsigned int check(void)
{
    unsigned int input;
    char ch;

    while (scanf("%u", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch); /*处理错误输入*/
        printf(" is not an integer.Please enter a right number"
               " (like1,50 or 8950,but less than 4294967295:");
    }

    return input;
}

方法三:递归求解

#include <stdio.h>
#include <stdlib.h>
#define upper 57
unsigned int Fibonacci(unsigned int a); /*打印Fibonacci*/
unsigned int check(void);               /*检查输入是否为数字的错误*/

int main(void)
{
    unsigned int num; /*Fibonacci数列的项数*/
    printf("This program is using for Fibonacci array.\n");
    printf("Please enter the number that you want to see the "
           "Fibonacci(more than 0,less than 4294967295)"
           " (For your computer's speed of operation considerations,"
           "generally do not recommend more than 10000):");
    /*因为用的是unsigned int类型,所以建议输入大于0小于4294967295*/
    while (1) /*无条件进入循环*/
    {
        num = check();
        for (int i = 1; i <=num; i++)
        {
            printf("%u  ", Fibonacci(i));
        }

        /*准备开始第n(n>1)次循环*/
        printf("This program is using for Fibonacci array.\n");
        printf("Please enter the number that you want to see "
               "the Fibonacci(less than 4294967295)"
               " (For your computer's speed of operation "
               "considerations, generally do not recommend "
               "more than 10000)");
    }
    printf("Done!");

    return EXIT_SUCCESS;
}

unsigned int Fibonacci(unsigned int a) /*Fibonacii数列求解运算*//*递归*/
{
    if (a > 2)  /*F(n)=F(n-1)+F(n-2)*/
        return Fibonacci(a - 1) + Fibonacci(a - 2);
    else
        return 1;
}

unsigned int check(void)
{
    unsigned int input;
    char ch;

    while (scanf("%u", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch); /*处理错误输入*/
        printf(" is not an integer.Please enter a right number"
               " (like1,50 or 8950,but less than 4294967295:");
    }

    return input;
}

核心代码:
普通法:

unsigned long Fibonacci(unsigned n)
{
    int a = 1, b = 1;
    int i, temp;

    if (n > 2)
        for (i = 3; i <= n; i++)
        {
            temp = a + b; /*交换数值*/
            a = b;
            b = temp;
        }
    else
        b = 1;

    return b;
}

数组法:

unsigned int Fibonacci(unsigned int a) /*Fibonacii数列求解运算*/
{
    unsigned int arr[a];
    arr[0] = 1;
    arr[1] = 1;
    if (a > 2) /*求出第3项及以后的结果*/
    {
        printf("%u %u", arr[0], arr[1]);
        for (int i = 2; i < a; i++)
        {
            arr[i] = arr[i - 1] + arr[i - 2];
            printf("  %u", arr[i]);
            if (i == (a - 1))
            {
                printf("\n");
            }
        }
    }
    else /*处理当输入1 or 2时的情况*/
    {
        if (a == 1)
        {
            printf("%u\n", arr[1]);
        }
        if (a == 2)
        {
            printf("%u %u\n", arr[1], arr[1]);
        }
    }
}

递归法:

unsigned int Fibonacci(unsigned int a) /*Fibonacii数列求解运算*//*递归*/
{
    if (a > 2)  /*F(n)=F(n-1)+F(n-2)*/
        return Fibonacci(a - 1) + Fibonacci(a - 2);
    else
        return 1;
}

显而易见的是,普通法最简单,但代码可读性不高;数组法难易度居中;递归法最难,代码最简洁,但同时占用内存也多。

  • 10
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值