当前位置:网站首页>[The sword refers to the offer-Nioke.com brush questions] The first article - Fibonacci sequence - C implementation
[The sword refers to the offer-Nioke.com brush questions] The first article - Fibonacci sequence - C implementation
2022-08-11 11:58:00 【aaaafeng】

Preface
Blogger homepage: Aaaafeng's homepage_CSDN
Niuke.com is a very useful tool for brushing questions. I recommend it to everyone:
Website link: Niuke.com
This series of blog posts is used as a notebook for writing questions, recording the thinking process of individuals facing some programming problems.If you have any questions, welcome to discuss and correct!Hope to work hard with you and grow together!
Article table of contents
Problem description

Original title link: Fibonacci sequence
Solution ideas
Looking at the expression of
fib(x), it is easy to see that this problem is very suitable for using the function's recursion to solve.Because its expression itself is also a recursive form.
如果对斐波拉契数列还不熟悉,可以试着将一些项列出来:1,1,2,3,5,8,13,21,34,55,89······
刚开始接触递归的时候,可能会莫名德就感觉它非常的难以理解,但其实没关系,多做一些题,自己多用几次,自然慢慢得就熟了。
Algorithm process:
For an x passed in by the function
fib(n):
1) Determine whether n is 1 or 2, if so, return 1 directly
2) If not, then进入递归,先计算第n 项的前两项(第n-1 和第n-2 项),然后将它们的和作为值返回
在第二步When calculating the first two items, it is a two-step process of 1) and 2), which constitutes recursion
Core code implementation:
Note: This is just the implementation of the core code. As a function to complete the main algorithm task, it is not a complete runnable code.Call this function in the main function to complete the corresponding algorithm task.
int Fibonacci(int n ) {// write code hereif(n == 1 || n == 2)return 1;return Fibonacci(n - 1) + Fibonacci(n - 2);}
Summary
The implementation of this algorithm is still very simple. When we encounter some more complex recursive tasks, we can actually refer to the previous simple programs in turn, which may make our thinking clearer.
Let's work together!
边栏推荐
猜你喜欢

【深度学习】笔记2-模型在测试集的准确率大于训练集

vending machine

PL4807-ADJ线性锂电池可调充电芯片
![[10点公开课]:AV1编码器的优化及其在流媒体和实时通讯中的应用](/img/86/a6cd309cd66eb37159fcb8ae3338b1.png)
[10点公开课]:AV1编码器的优化及其在流媒体和实时通讯中的应用

陕西cas:2055042-71-0N-(炔-四聚乙二醇)-生物素价格

简单记录openguass_exporter对接prometheus通过grafanai来实现可视化监控

【医学统计学】二项分布

EXCLUSIVE INTERVIEW | INTELLIGENCE IS SPONTANED, NOT PLANNED: Evolution Fan, Former OpenAI Research Manager and UBC Associate Professor Jeff Clune

VirtualLab: Ince - array of laser Gaussian beam generated vortex observation

Grid 布局介绍
随机推荐
Jmeter性能测试
【毕业设计】远程智能浇花灌溉系统 - stm32 单片机 嵌入式 物联网
六月成功案例
关于b/s技术的一些思考--模板技术
Network Security - nmap
【深度学习】小结1-入门两周学习感受
闪灯芯片银行塔闪灯IC参数应用
爆赞!阿里P8首次分享出基于Docker的企业级Redis实战开源笔记
Go编译原理系列10(逃逸分析)
C语言,怪题小谈
【五一特刊】FPGA零基础学习:VGA协议驱动设计
易观分析:银行零售业务实现智能化营销还需突破七大关键点
谷歌搜索,全球宕机??
FS2956A 输入8-120V 用于液晶仪表5V-USB 充电口方案
公共管理学选择题(最终版)
集合面试题
Kubernetes应用发布思路分析
Volatile关键字的作用
img图片加载错误时显示默认图片
图像识别(八)| 还对全连接层迷迷糊糊?背会一首诗就行了