当前位置:网站首页>【斐波那契数列】
【斐波那契数列】
2022-06-11 01:43:00 【爱吃榴莲的喵星人】
1.基本介绍
斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
提示:这个数列从第3项开始,每一项都等于前两项之和。
2.递归实现
#include<stdio.h>
int Fib(int n)
{
if (n <= 2)
return 1;
else
return Fib(n - 1) + Fib(n - 2);
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = Fib(n);
printf("%d\n", ret);
return 0;
}
运行结果

时间复杂度:O(2^n)
空间复杂度:O(n)
提示:空间可以重复利用,不累计的;时间是一去不复返,累计的
3.非递归实现
迭代实现
提示:时间复杂度:O(n) ; 空间复杂度O(1)
#include<stdio.h>
int Fib(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = Fib(n);
printf("%d\n", ret);
return 0;
}
运行结果
数组实现
提示:时间复杂度:O(n) ; 空间复杂度O(n)
#include<stdio.h>
#include<stdlib.h>
long long Fibonacci(unsigned int n)
{
if (n == 0)
return 0;
long long ret=0;
long long* fibArray = (long long*)malloc((n + 1) * sizeof(long long));
if (fibArray == NULL)
{
printf("空间开辟失败");
}
else
{
fibArray[1] = 1;
fibArray[2] = 1;
for (long long i = 3; i <= n; i++)
{
fibArray[i] = fibArray[i - 1] + fibArray[i - 2];
}
ret = fibArray[n];
free(fibArray);
fibArray = NULL;
}
return ret;
}
int main()
{
unsigned int n = 0;
scanf("%d", &n);
long long ret = Fibonacci(n);
printf("%lld\n", ret);
return 0;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether
- MySQL backup and recovery
- 3P5 Industrial Engineering Lecture 1-2: Method of Study
- P4338 [ZJOI2018]历史(树剖)(暴力)
- String operation methods: replace, delete and split strings
- clang-format 最全格式说明
- Wechat automatic red envelope grabbing source code
- Jetpack compose box control
- 优化调度(火电、风能、储能)【matlab代码实现】
- Xampp is used under M1 chip, and the installation extension error
猜你喜欢

378. 有序矩阵中第 K 小的元素

During SSH configuration key login, you need to pay attention to whether the private key is set with a password

力扣刷题篇——哈希表

Test questions and answers of 2022r1 quick opening pressure vessel operation certificate

The most complete format description of clang format

10 years of domestic milk powder counter attack: post-90s nannies and dads help new domestic products counter attack foreign brands

Why is the trend chart of precious metal silver strong?
![[AI weekly] AI and freeze electron microscopy reveal the structure of](/img/2e/e986a5bc44526f686c407378a9492f.png)
[AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso

扁平数据转tree与tree数据扁平化

当逻辑删除遇上唯一索引,遇到的问题和解决方案?
随机推荐
APP测试_测试点总结
Jetpack compose box control
The diligent is the laziest
Project load failed
378. the k-th smallest element in an ordered matrix
叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物
Closing method of SQL injection
SQL | 外连接
InfoQ geek media's 15th anniversary solicitation | in depth analysis of container runtime Technology
[3.delphi common components] 7 timer
UI interaction
What do you know about the set class? Soul questions from Volume I
Introduction for i-Teams
10007. ISBN号码
Dynamically add attributes to objects
How to guarantee the data quality of data warehouse?
Is it appropriate for a 27 - year-old girl to change her career from zero to software testing?
扁平数据转tree与tree数据扁平化
Whether the software test needs to master the programming ability
[3.delphi common components] 6 scroll bar


