当前位置:网站首页>【函数递归】简单递归的5个经典例子,你都会吗?
【函数递归】简单递归的5个经典例子,你都会吗?
2022-07-07 18:25:00 【猪皮兄弟】
🧸🧸🧸各位巨佬大家好,我是猪皮兄弟🧸🧸🧸
今天和大家谈谈简单递归🥳🥳🥳
什么是递归
递归的定义:
递归是一种解决问题的有效方法,在递归过程中,函数将自身作为子例程调用。
也就是说说程序调用自身的编程技巧叫递归。递归的思想是把一个大型复杂问题层层转化为一个与原问题规模更小的问题,问题被拆解成子问题后,递归调用继续进行,直到子问题无需进一步递归就可以解决的地步为止。
递归的重要思想就是大化小
递归最重要的就是需要避免死循环的出现和逻辑关系的正确性
递归经典例题
1.递归模拟实现库函数strlen
代码如下:
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;
}
需要注意的就是递归的截止条件
2.递归模拟实现字符串的逆置
代码如下:
#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;
//需要置换到后半部分的字符,每次递归都会创建一个temp存起来,
//递归结束后开始回溯时才将他赋值给后半部分,因为首先要对后半
//部分进行一个置'\0'
}
int main()
{
char str[] = "abcdef";
reverse_string(str);
puts(str);
return 0;
}
3.斐波那契数列的递归和非递归表示
代码如下:
}
int fib(int n)
{
递归
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fib(n - 1) + fib(n - 2);
}
//因为斐波那契如果用递归的话效率太低了,所以用非递归
//非递归
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.递归青蛙跳台阶问题
代码如下:
//青蛙跳台阶
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.递归汉诺塔问题
代码如下:
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;
}
肺腑之言
今天的分享就到这里,以后还会持续不断的更新的!!谢谢大家的支持
边栏推荐
- OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
- MRS离线数据分析:通过Flink作业处理OBS数据
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Tensorflow2.x下如何运行1.x的代码
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- [solution] package 'XXXX' is not in goroot
- 【网络原理的概念】
- Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
- [philosophy and practice] the way of program design
- Vulnhub's funfox2
猜你喜欢

写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!

Mongodb learn from simple to deep

I Basic concepts

Jenkins 用户权限管理

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

Apifox interface integrated management new artifact

【哲思与实战】程序设计之道

Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system

Vulnhub's funfox2

PHP method of obtaining image information
随机推荐
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
想杀死某个端口进程,但在服务列表中却找不到,可以之间通过命令行找到这个进程并杀死该进程,减少重启电脑和找到问题根源。
使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
Nebula Importer 数据导入实践
CJSON内存泄漏的注意事项
guava多线程,futurecallback线程调用不平均
TS快速入门-泛型
With st7008, the Bluetooth test is completely grasped
Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed
Apifox 接口一体化管理新神器
Mongodb由浅入深学习
Vulnhub tre1
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
有用的win11小技巧
MSE API learning
【mysql篇-基础篇】事务
Get webkitformboundary post login
Micro service remote debug, nocalhost + rainbow micro service development second bullet
POJ 1742 coins (monotone queue solution) [suggestions collection]
Cantata9.0 | 全 新 功 能