当前位置:网站首页>【函数递归】简单递归的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;
}
肺腑之言
今天的分享就到这里,以后还会持续不断的更新的!!谢谢大家的支持
边栏推荐
- POJ 1742 coins (monotone queue solution) [suggestions collection]
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Force buckle 1961 Check whether the string is an array prefix
- Oracle 存儲過程之遍曆
- 基于深度学习的目标检测的更新迭代总结(持续更新ing)
- POJ 1742 Coins ( 单调队列解法 )「建议收藏」
- CJSON内存泄漏的注意事项
- Graduation season | regretful and lucky graduation season
- [philosophy and practice] the way of program design
- Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
猜你喜欢

Cantata9.0 | 全 新 功 能

MRS离线数据分析:通过Flink作业处理OBS数据

Measure the height of the building

不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统

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

PHP method of obtaining image information
![嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]](/img/af/61b384b1b6ba46aa1a6011f8a30085.png)
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]

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

Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city

AADL Inspector 故障树安全分析模块
随机推荐
取两个集合的交集
Traversal of Oracle stored procedures
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
Yolov6:yolov6+win10--- train your own dataset
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
基于深度学习的目标检测的更新迭代总结(持续更新ing)
Mongodb由浅入深学习
Network principle (1) - overview of basic principles
Gorilla official: sample code for golang to open websocket client
【解决】package ‘xxxx‘ is not in GOROOT
VMWare中虚拟机网络配置
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
字符串中数据排序
[résolution] le paquet « xxxx» n'est pas dans goroot
Referrer和Referrer-Policy简介
Mongodb learn from simple to deep
guava多线程,futurecallback线程调用不平均
c语言如何判定是32位系统还是64位系统