当前位置:网站首页>[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!
边栏推荐
- OpenHarmony如何选择图片在Image组件上显示(eTS)
- PL4807-ADJ线性锂电池可调充电芯片
- 【五一特刊】FPGA零基础学习:VGA协议驱动设计
- 锂电池充电芯片IC
- 案例复现,带你分析Priority Blocking Queue比较器异常导致的NPE问题
- error: ‘const char* libc_name_p(const char*, unsigned int)’ redeclared inline with ‘gnu_inline’ attr
- 98转出0转入,985高校土木工程沦为“天坑”引热议
- 威佐夫博弈
- From Douyin to Volcano Engine——Seeing the Evolution and Opportunities of Streaming Media Technology
- 从抖音到火山引擎——看流媒体技术演进和机会
猜你喜欢
随机推荐
SpinalHDL资料汇总
目标检测学习笔记——paddleDetection使用
98转出0转入,985高校土木工程沦为“天坑”引热议
SM5200原厂SOT23-6 500mA 线性锂电子替代芯片
Grid 布局介绍
路测数据合并
【集创赛】arm杯一等奖作品:智能BLDC驱动系统
二叉树详解
威佐夫博弈
易观分析:银行零售业务实现智能化营销还需突破七大关键点
鸿海董事长刘扬伟:市场对智能手机和其他消费电子产品的需求正在放缓
Five minutes to teach you intranet penetration
低延时实时音视频在5G远程操控场景的应用实践
微服务分布式构架开发实战PDF,阿里架构师推荐,快快收藏吧
EastWave应用:负折射现象实时演示
全网最全的权限系统设计方案(图解)
公共管理学选择题(最终版)
云原生(三十四) | Kubernetes篇之平台存储系统实战
A043:做好芯片设计,这三种能力不可或缺
Tool_RE_IDA基础字符串修改