当前位置:网站首页>Recursion and non recursion are used to calculate the nth Fibonacci number respectively
Recursion and non recursion are used to calculate the nth Fibonacci number respectively
2022-07-28 03:43:00 【Rain_ too】
First , You need to know what is Fibonacci sequence ?
Fibonacci sequence :
Mathematically, it is defined by recursion :F(0) = 0,F(1) = 1, F(n) = F(n - 1) + F(n - 2)(n ≥ 2,n ∈ N * )
0、1、1、2、3、5、8、13、21、34、……
Recursive writing
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int F(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return F(n - 1) + F(n - 2);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = F(n);
printf("%d\n", ret);
return 0;
}Have you found any problems with this code , That is when n When the value of is large , There will be many repeated calculations , When n=30 when ,F(0) Will be recalculated 514229 Time ; When n=40 when ,F(0) Will be recalculated 63245986 Time ( It takes six or seven seconds to calculate ); When n=50 when , You will find that the calculation results can't come out for a long time ; Low code efficiency .
At this time, we can consider using the iterative method .
Iterative writing
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int F(int n)
{
int a = 0;
int b = 1;
int c = 0;
if (n == 0)
{
return 0;
}
else if(n == 1)
{
return 1;
}
while (n > 1)
{
c = a+b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = F(n);
printf("%d\n", ret);
return 0;
}边栏推荐
- 高等数学(第七版)同济大学 习题3-6 个人解答
- 【P4】 查看库文件两个历史版本的区别
- [openvx] VX for basic use of objects_ distribution
- Play WolframAlpha computing knowledge engine
- Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
- 高等数学(第七版)同济大学 习题3-5 个人解答
- After reading MySQL database advanced practice (SQL xiaoxuzhu)
- Integrate SSM to realize search of addition, deletion, modification and query
- ES6 from getting started to mastering 09: symbol type
- An article grasps the calculation and processing of date data in PostgreSQL
猜你喜欢

2022 summary of the latest Android handler related interview questions

Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse

数据丰富的计算:M.2在边缘遇到AI

单调栈——42. 接雨水——面大厂必须会的困难题

Mouse operation and response

高等数学(第七版)同济大学 习题3-5 个人解答

Asemi rectifier bridge gbpc5010, gbpc5010 parameters, gbpc5010 size
D2DEngine食用教程(4)———绘制文本

SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版

高等数学(第七版)同济大学 习题3-6 个人解答
随机推荐
Mouse operation and response
C语言力扣第45题之跳跃游戏 II。遍历跳跃
Unity backpack system
leetcode刷题:动态规划08(分割等和子集)
What is tor? What is the use of tor browser update?
12月份PMP考试首次采用新考纲,该怎么学?
2022 summary of the latest Android handler related interview questions
收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小
8000字讲透OBSA原理与应用实践
我的创作纪念日
How to uninstall clean ZABBIX service? (super detailed)
LightPicture – 精致图床系统
MSGAN用于多种图像合成的模式搜索生成对抗网络---解决模式崩塌问题
Screenshot of deepstream detection results
Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
pip-script. py‘ is not present Verifying transaction: failed
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning
【论文笔记】基于深度学习的移动机器人自主导航实验平台
动态规划——509. 斐波那契数