当前位置:网站首页>[C language series] - a recursive topic
[C language series] - a recursive topic
2022-07-29 05:35:00 【Gancheng なつき】
꧁ Hello, everybody ! It is a great honor to have your visit , Let's have a long way to go in programming !꧂
* Blog column :【C All living things 】*
Introduction to this article : Simply tear a recursive topic by hand !
Get to know the author : Aspire to be a great programmer , Xiaobai, who is currently a sophomore in College .
Inspirational terms : The tediousness of programming , Let's study together and become interesting !
Text begins
List of articles
Preface
In a scene yesterday C In the preliminary language examination , Fortunately, I came across a problem about recursion , The problem I couldn't do at that time , But with years of guessing experience , Very lucky to guess the right answer !
subject
After the following procedure is executed , What is the result of the output ?
#include<stdio.h>
int cnt = 0;
int fib(int n)
{
cnt++;
if (n == 0)
return 1;
else if (n == 1)
return 2;
else
return fib(n - 1) + fib(n - 2);
}
int main()
{
fib(8);
printf("%d", cnt);
return 0;
}
Ideas
To write this problem, you must understand the concept and usage of recursive functions !
Now let's analyze this problem :
1. Global variables are defined cnt Used to count , Every time you call a function ,cnt Meeting ++ once , The main function passes a 8 Get into fib() function ;
After analysis, this problem actually requires to find out that when the transmission parameter is 8 when , Called fib() The number of functions !
2. Drawing pictures is more conducive to solving problems !
Graphic process
Let's run it to see if 67 Time !
Conclusion
well ! This is the end of my sharing today , If you think it helps , You can pay attention to bloggers ! Bloggers will update regularly !
边栏推荐
猜你喜欢
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
游戏行业弹性计算最佳实践
【活动预告】云上数字工厂与中小企业数字化转型创新论坛
省市区三级联动(简单又完美)
微信小程序-屏幕高度
Longest string without duplicate characters
Day 3
Day 2
牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
[event preview] cloud digital factory and digital transformation and innovation forum for small and medium-sized enterprises
随机推荐
基础爬虫实战案例之获取游戏商品数据
Day 1
link与@import导入外部样式的区别
Best practices for elastic computing in the game industry
Niuke network programming problem - [wy22 Fibonacci series] and [replace spaces] detailed explanation
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
数据库操作 Day 6
个人学习笔记
paddle. Fluid constant calculation error 'nonetype' object has no attribute 'get_ fetch_ list‘
【C语言系列】— 打印100~200之间的素数
B - identify floating point constant problems
Day 5
冒泡排序 C语言
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
力扣994:腐烂的橘子(BFS)
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
阿里云架构师细说游戏行业九大趋势
Clickhouse learning (V) cluster operation
Occt learning 003 - MFC single document project
Detailed explanation of serial port communication