当前位置:网站首页>【函数递归】简单递归的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;
}
肺腑之言
今天的分享就到这里,以后还会持续不断的更新的!!谢谢大家的支持
边栏推荐
- 《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
- Force buckle 1790 Can two strings be equal by performing string exchange only once
- 【mysql篇-基础篇】事务
- 【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- Force buckle 989 Integer addition in array form
- Gorilla official: sample code for golang to open websocket client
- PHP method of obtaining image information
- Oracle 存储过程之遍历
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
- Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system
猜你喜欢
机械臂速成小指南(十一):坐标系的标准命名
Machine learning notes - explore object detection datasets using streamlit
I Basic concepts
【哲思与实战】程序设计之道
CodeSonar如何帮助无人机查找软件缺陷?
测量楼的高度
One click deployment of any version of redis
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
MRS离线数据分析:通过Flink作业处理OBS数据
Network principle (1) - overview of basic principles
随机推荐
Traversal of Oracle stored procedures
基于深度学习的目标检测的更新迭代总结(持续更新ing)
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
[concept of network principle]
Force buckle 912 Sort array
Creation of kubernetes mysql8
With st7008, the Bluetooth test is completely grasped
Useful win11 tips
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
php 获取图片信息的方法
Force buckle 674 Longest continuous increasing sequence
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
Solve the problem that the executable file of /bin/sh container is not found
Spark 判断DF为空
Flask1.1.4 Werkzeug1.0.1 源码分析:路由
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!