当前位置:网站首页>[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 
边栏推荐
- Introduction to referer and referer policy
- [solution] package 'XXXX' is not in goroot
- Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed
- Mrs offline data analysis: process OBS data through Flink job
- 开发一个小程序商城需要多少钱?
- Guava multithreading, futurecallback thread calls are uneven
- Is it safe to open a stock account at present? Can I open an account online directly.
- JNI 初级接触
- 如何满足医疗设备对安全性和保密性的双重需求?
- Spark judges that DF is empty
猜你喜欢
Codesonar enhances software reliability through innovative static analysis

Static analysis of software defects codesonar 5.2 release

Codesonar Webinar

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

Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system

目标:不排斥 yaml 语法。争取快速上手

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

Implement secondary index with Gaussian redis

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

Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
随机推荐
Cantata9.0 | 全 新 功 能
恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
Mongodb learn from simple to deep
刚开户的能买什么股票呢?炒股账户安全吗
You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
测量楼的高度
2022如何评估与选择低代码开发平台?
有用的win11小技巧
guava多线程,futurecallback线程调用不平均
How does codesonar help UAVs find software defects?
Cantata9.0 | new features
【解决】package ‘xxxx‘ is not in GOROOT
Precautions for cjson memory leakage
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
使用高斯Redis实现二级索引
Update iteration summary of target detection based on deep learning (continuous update ing)
EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
怎样用Google APIs和Google的应用系统进行集成(1)—-Google APIs简介
CodeSonar如何帮助无人机查找软件缺陷?
智能软件分析平台Embold