当前位置:网站首页>[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
- 写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
- 理财产品要怎么选?新手还什么都不懂
- How does codesonar help UAVs find software defects?
- 机械臂速成小指南(十二):逆运动学分析
- 恶魔奶爸 B2 突破语法,完成正统口语练习
- Deep learning model compression and acceleration technology (VII): mixed mode
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- 《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
猜你喜欢

Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)

机械臂速成小指南(十二):逆运动学分析

How to meet the dual needs of security and confidentiality of medical devices?

神兵利器——敏感文件发现工具

Splicing and splitting of integer ints

恶魔奶爸 B3 少量泛读,完成两万词汇量+

Apifox 接口一体化管理新神器

Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
随机推荐
使用camunda做工作流设计,驳回操作
静态测试工具
【函数递归】简单递归的5个经典例子,你都会吗?
Alibaba cloud award winning experience: how to mount NAS file system through ECS
[résolution] le paquet « xxxx» n'est pas dans goroot
恶魔奶爸 B2 突破语法,完成正统口语练习
Écrivez une liste de sauts
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
AIRIOT助力城市管廊工程,智慧物联守护城市生命线
华为CE交换机下载文件FTP步骤
Spark judges that DF is empty
I Basic concepts
POJ 1742 Coins ( 单调队列解法 )「建议收藏」
使用 BR 恢复 Azure Blob Storage 上的备份数据
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
使用高斯Redis实现二级索引
Dachang classic pointer written test questions
Mongodb learn from simple to deep