当前位置:网站首页>On the non recursive and recursive implementation of finding the nth Fibonacci number respectively
On the non recursive and recursive implementation of finding the nth Fibonacci number respectively
2022-07-28 17:45:00 【Crazy orange】
Fibonacci sequence :1,1,2,3,5,8,13,21,34......
The first two numbers are 1, When n>=2 when , hinder Fibonacci The number is the sum of the first two Fibonacci numbers .
Non recursive method to achieve N Fibonacci Numbers
#include <stdio.h>
int main()
{
int i = 0;
int a = 1;
int b = 1;
int c = a + b;
int n = 2;
scanf("%d", &n);
if (n > 2)//n<3 The output 1,n>3 Enter circulation
{
for (i = 1; i <= n - 3; i++)// As the first 4 Fibonacci Numbers , Only bad circulation 1 Time .
{
a = b; //a=1
b = c; //b=2
c = a + b; //c=3
}
printf("%d\n", c);// Output No 4 Fibonacci Numbers 3
}
else
printf("%d\n", 1);//
return 0;
}The recursive method realizes the N Fibonacci Numbers
#include <stdio.h>
int Fibonacci_number(int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
{
return 1;
}
else
return Fibonacci_number(n - 1) + Fibonacci_number(n - 2);
}
int main()
{
int n = 0;
scanf("%d",&n);
printf(" The first %d The Fibonacci number is %d\n", n, Fibonacci_number(n));
return 0;
}Recursive drawing demonstration
If scanf Input n yes 4, Get into Fibonacci_number(n) Function body

After calling the function ,return Return value , First return the last recursive function Fibonacci_number(n - 1) The value of is 1, Fibonacci_number(n - 2) yes 1,1+1=2; Return to the second recursive return 2、1, The final return value of the main function is 3.
Final output : The first 4 The Fibonacci number is 3.

边栏推荐
- 7-8 浪漫侧影(25分)建树+新解题思路
- 新手通过自学转行软件测试难度大吗?
- Kali installation configuration of penetration test killer
- 想转行IT,非科班出身真的不要紧吗?
- Jdwp unauthorized rapid utilization
- 【p5.js】实战练习——无规则对称
- Technical aspects passed easily, HR: those with only three years of experience in large factories are not worth 20K
- MySQL高级-MVCC(超详细整理)
- 关于非递归和递归分别实现求第n个斐波那契数
- Division optimization of JS decimal calculation on the Internet
猜你喜欢

【C语言笔记分享】自定义类型:结构体,枚举,联合(建议收藏)
![[阅读笔记] For Paper:R-CNN系列的三篇论文总结](/img/1d/8b862ac66ea04d9371f64e35fc4396.png)
[阅读笔记] For Paper:R-CNN系列的三篇论文总结

Visual Object Class介绍PASCAL VOC数据集

Mmcv installation method

easyui tree

软件测试前景如何?该如何进行学习呢?

In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction

生信人的20个R语言习题

Random talk on test platform - platform construction ideas (Part 1)

Talk about the measurement of "post release problems"
随机推荐
软件测试和软件开发应该怎么选择?
软件测试培训两个月可以就业吗?
编译原理学习笔记3(自上而下语法分析)
电工学自学笔记1.22
R language sub() usage
解决Package is not available (for R ve【PACKAGE ‘XXX’ IS NOT AVAILABLE (FOR R VERSION X.Y.Z)” WARNING?】
100+医学影像数据集集锦
Arya-专业web自动化测试平台
Talking about test platform -- Discussion on construction mode
mmcv安装的办法
Jerry ac1082/1074/1090 development record
电工学自学笔记1.20
ROS system installation
软件测试前景如何?该如何进行学习呢?
[阅读笔记]-2 通过朴素贝叶斯模型学习机器学习分类
ggplot2地图
PyTorch中grid_sample的使用方法
JS synchronizes the local time with the server time
Distinguish between the export of ES6 and the module.exports of nodejs
ROS系统安装