当前位置:网站首页>[function recursion] do you know all five classic examples of simple recursion?
[function recursion] do you know all five classic examples of simple recursion?
2022-07-07 20:31:00 【Pigskin brother】
🧸🧸🧸 Hello, guys , I'm pig skin brother 🧸🧸🧸
Today I want to talk about simple recursion 🥳🥳🥳
What is recursion
Definition of recursion :
Recursion is an effective way to solve problems , In the process of recursion , The function calls itself as a subroutine .
That is to say, the programming skill of program call itself is called recursion . The idea of recursion is to transform a large and complex problem into a smaller problem than the original problem , After the problem is broken down into sub problems , The recursive call continues , Until the subproblem can be solved without further recursion .
The important idea of recursion is to enlarge and reduce
The most important thing about recursion is We need to avoid the occurrence of dead cycles and Correctness of logical relationship
Classic examples of recursion
1. Recursive simulation of library functions strlen
The code is as follows :
int my_strlen(char* str)
{
if (*str == '\0')
return 0;
else
{
return 1 + my_strlen(++str);
}
}
int main()
{
char str[] = "abcdef";
printf("%d",my_strlen(str));
return 0;
}
What we need to pay attention to is the cut-off condition of recursion
2. Recursive simulation realizes the inverse of string
The code is as follows :
#include <string.h>
int my_strlen(const char* str)
{
if (*str == 0)
{
return 0;
}
else
{
return 1 + my_strlen(++str);
}
}
void reverse_string(char*str)
{
int len = my_strlen(str);
char* temp = *str;
*str = *(str + len - 1);
*(str + len - 1) = 0;
if (my_strlen(str + 1) >= 2)
reverse_string(str+1);
*(str + len - 1) = temp;
// Characters that need to be replaced to the second half , Each recursion creates a temp Save up ,
// It is assigned to the second half only when the recursion ends and the backtracking begins , Because first of all, we have to deal with the second half
// Part of a set '\0'
}
int main()
{
char str[] = "abcdef";
reverse_string(str);
puts(str);
return 0;
}
3. Recursive and non recursive representations of Fibonacci sequences
The code is as follows :
}
int fib(int n)
{
recursive
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fib(n - 1) + fib(n - 2);
}
// Because Fibonacci is too inefficient if it uses recursion , So use non recursive
// Non recursive
int a = 1, b = 1, c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",fib(n));
return 0;
}
4. Recursive frog jumping steps
The code is as follows :
// Frogs jump the steps
int JumpFloor(int n)
{
if (n == 1)
return 1;
else if (n == 2)
return 2;
else
{
return JumpFloor(n - 1) + JumpFloor(n - 2);
}
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",JumpFloor(n));
return 0;
}
5. Recursive Hanoi Tower problem
The code is as follows :
void move(pos1, pos2)
{
printf("%c--->%c\n",pos1,pos2);
}
void hanio(int n,char pos1,char pos2,char pos3)
{
if (n == 1)
{
printf("%c--->%c\n",pos1,pos3);
}
else
{
hanio(n - 1, pos1, pos3, pos2);
move(pos1, pos3);
hanio(n - 1, pos2, pos1, pos3);
}
}
int main()
{
int n;
scanf("%d",&n);
char pos1 = 'A', pos2 = 'B', pos3 = 'C';
hanio(n,pos1,pos2,pos3);
return 0;
}
habla mi corazon
Today's sharing is here , It will be updated continuously in the future !! Thank you for your support
边栏推荐
- Apifox interface integrated management new artifact
- 恶魔奶爸 A0 英文零基础的自我提升路
- [paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
- When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
- Data sorting in string
- Intelligent software analysis platform embold
- Apifox 接口一体化管理新神器
- 恢复持久卷上的备份数据
- 恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
- 恶魔奶爸 C
猜你喜欢
测量楼的高度
One click deployment of any version of redis
如何满足医疗设备对安全性和保密性的双重需求?
Codesonar Webinar
Tensorflow2. How to run under x 1 Code of X
恶魔奶爸 B3 少量泛读,完成两万词汇量+
让这个CRMEB单商户微信商城系统火起来,太好用了!
使用高斯Redis实现二级索引
CodeSonar通过创新型静态分析增强软件可靠性
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
随机推荐
Mongodb learn from simple to deep
九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
Alibaba cloud award winning experience: how to mount NAS file system through ECS
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
Codesonar Webinar
深度学习模型压缩与加速技术(七):混合方式
【C语言】指针进阶---指针你真的学懂了吗?
2022如何评估与选择低代码开发平台?
C语言多角度帮助你深入理解指针(1. 字符指针2. 数组指针和 指针数组 、数组传参和指针传参3. 函数指针4. 函数指针数组5. 指向函数指针数组的指针6. 回调函数)
Nebula importer data import practice
Apifox 接口一体化管理新神器
How to meet the dual needs of security and confidentiality of medical devices?
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
[résolution] le paquet « xxxx» n'est pas dans goroot
Is it safe to open a stock account at present? Can I open an account online directly.
Update iteration summary of target detection based on deep learning (continuous update ing)