方法有三种甚至更多,但核心思想都是一个数列的通项公式: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;}elseb = 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);elsereturn 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;}elseb = 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);elsereturn 1;
}

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

C语言求Fibonacci数列相关推荐

  1. c语言求数列的和_例15:C语言求Fibonacci数列的前30个数

    例15:求Fibonacci数列的前30个数.这个数列有以下特点:第1,2两个数为1,1,.从第三个数开始,该数是其前两个数之和.(斐波那契不死神兔) 解题思路:从前两个月的兔子数可以推出第3个月的兔 ...

  2. c语言求fibonacci数列前20,求fibonacci数列的前20个数之和

    使用数组求Fibonacci数列的前20项.要求4项一行输出. 斐波那契数列通项公式:斐波那契数列指的是这样一个数列:1.1.2.3.5.8.13.21.--这个数列从第三项开始,每一项都等于前两项之 ...

  3. matlab求斐波那契数列第n项的值,求fibonacci数列第n项的值. 1 1 2 3 5 8....n ?

    [C语言]用递归算法编写一个程序求Fibonacci数列的第n项值 #includeunsignedintFibonacci(intn);intmain(void){inti;for(i=1;i vb ...

  4. C语言试题五十九之请编写一个函数fun,它的功能时:求fibonacci数列中大于t的最小的一个数,结果由函数返回。

    1. 题目 请编写一个函数fun,它的功能时:求fibonacci数列中大于t的最小的一个数,结果由函数返回. 其中fibonacci数列f(n)的定义为:f(0)=0,f(1)=1,f(n)=f(n ...

  5. 用php递归求fibonacci数列,C++_C语言求Fibonacci斐波那契数列通项问题的解法总结,一:递归实现使用 - phpStudy...

    C语言求Fibonacci斐波那契数列通项问题的解法总结 一:递归实现  使用公式f[n]=f[n-1]+f[n-2],依次递归计算,递归结束条件是f[1]=1,f[2]=1. 二:数组实现  空间复 ...

  6. 求Fibonacci数列的前20项

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p112 习题4 2.编程求Fibonacci数列的前20项. Fibonacci数列的定义:F0=0, ...

  7. C++项目參考解答:求Fibonacci数列

    [项目:求Fibonacci数列] Fibonacci数列在计算科学.经济学等领域中广泛使用,其特点是:第一.二个数是1,从第3个数開始,每一个数是其前两个数之和.据此,这个数列为:1 1 2 3 5 ...

  8. 求 Fibonacci 数列的前 20 项

    求 Fibonacci 数列的前 20 项 #include <iostream>using namespace std;int main() {int f[20] = {1,1}; // ...

  9. 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]

    题目:定义Fibonacci数列如下: /  0                      n=0 f(n)=      1                      n=1         \  f ...

最新文章

  1. 中文NER任务简析与深度算法模型总结和实战展示 转 作者原创的不错,很有水平,需要研读
  2. PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历
  3. 广东工业大学计算机学院书记,计算机学院召开2018年工作总结大会
  4. HTTPS是如何工作的
  5. springboot集成redis_cluster两种方式
  6. linux 内网服务 端口,LINUX 内网设备将服务映射到公网地址
  7. 卫星定轨理论、GPS信号与卫星星历
  8. 前端50个精美登录注册模板(觉得好用帮我点个赞呗)
  9. 手把手学习Vue3.0:开发工具WebStorm和Vue模板文件介绍
  10. AVR单片机EEPROM存储空间的初始化
  11. 2021平凉二中高考成绩查询,平凉二中召开2020届高考启动暨教学工作推动会
  12. PS教程:磨砂颗粒质感字体海报设计
  13. 腾讯员工收入曝光,我顿悟了一个成人世界的残酷事实
  14. 042期正版四字梅花诗:冰清一洁
  15. Zip、Rar文件解压
  16. css基本样式表_基本的即用型CSS样式
  17. EasyPusher手机直播编码推送之图像旋转90度后画面重复的问题
  18. Python爬虫:Xpath爬取网页信息(附代码)
  19. 温故而知新--Java基础(三):Java常用集合类(上)
  20. DELPHI 文件操作 万一博客

热门文章

  1. react native 使用阿里字体图标库
  2. Android 画中画(图片)
  3. 关于城市旅游的HTML网页设计——中国旅游HTML+CSS+JavaScript 11页 带视频 带轮播
  4. react-native APP图标和名字的配置
  5. word2013图表题注:将图一-1改为图1-1
  6. 谈论AI之前,你搞懂人类了吗?
  7. SpringBoot整合MybatisPlus实战动态SQL,java分布式架构
  8. 三级分销如何做分销推广 如何设置分销比例
  9. python将两个csv文件按列合并
  10. vscode vue es6语法配置检测