当前位置:网站首页>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;
}边栏推荐
- 203. Remove linked list elements
- Push chart written by helm to harbor warehouse
- Interview essential skills: SQL query special training!
- D2dengine edible tutorial (4) -- draw text
- 基于SSM实现在线租房系统
- Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
- An article grasps the calculation and processing of date data in PostgreSQL
- Xctf attack and defense world web master advanced area unserialize3
- Practical scripts of mangopapa (contents)
- Container related concepts
猜你喜欢

MySQL Basics (create, manage, add, delete, and modify tables)

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

LabVIEW加载和使用树型控件项目中的定制符号

STM32 RT thread virtual file system mount operation
![[5g NR] RRC reject analysis](/img/51/fc39804b39a9014be3130c09e5444c.png)
[5g NR] RRC reject analysis

Interface automation test, complete introduction

Log analysis tool (Splunk)
![[paper notes] mobile robot autonomous navigation experimental platform based on deep learning](/img/6a/7f0c2b2a53332636f3172bc3b0b74d.png)
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning

【原型与原型链】初识原型与原型链~

Billions of asset addresses are blacklisted? How to use the tether address freezing function?
随机推荐
Interview essential skills: SQL query special training!
Differences among BRD, MRD and PRD
After 95, Alibaba P7 published the payroll: it's really heartbreaking
deepstream 检测结果截图
沃尔沃:深入人心的“安全感” 究竟靠的是什么?
In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
[wrong question]
ES6 从入门到精通 # 09:Symbol 类型
单调栈——42. 接雨水——面大厂必须会的困难题
Integrate SSM to realize search of addition, deletion, modification and query
[wrong question]mocha and railgun
leetcode刷题:动态规划08(分割等和子集)
203.移除链表元素
MySQL Basics (create, manage, add, delete, and modify tables)
Tungsten Fabric SDN — BGP as a Service
The open source of "avoiding disease and avoiding medicine" will not go far
数据挖掘-01
贪心——53. 最大子数组和
20220726 at command test of Bluetooth module hc-05 of Huicheng Technology
数据丰富的计算:M.2在边缘遇到AI