当前位置:网站首页>递归的基本原理
递归的基本原理
2022-07-29 05:08:00 【Ryan菲特】
递归的基本原理
来看一段程序:
//递归函数
#include <stdio.h>
void up_and_down(int);
int main(void)
{
up_and_down(1);
return 0;
}
void up_and_down(int n)
{
printf("Level %d : n location %p\n", n, &n);//#1
if(n < 4)
up_and_down(n + 1);
printf("LEVEL %d: n location %p\n", n, &n);//#2
}程序输出:
Level 1 : n location 000000000061fe00
Level 2 : n location 000000000061fdd0
Level 3 : n location 000000000061fda0
Level 4 : n location 000000000061fd70
LEVEL 4: n location 000000000061fd70
LEVEL 3: n location 000000000061fda0
LEVEL 2: n location 000000000061fdd0
LEVEL 1: n location 000000000061fe00
1.每级函数调用都有自己的变量
也就是说,第1级的n和第二级的n不同,所以程序创建了4个单独的变量,每个变量名都是n,但是它们的值各不相同
2.每次函数调用都会返回一次
当函数执行完毕后,控制权将被传回上一级递归。程序必须按顺序逐级返回递归,从某级up_and_down()返回上一级的up_and_down()。不能跳级回到main()中的第1级调用。
3.递归函数中位于递归调用之前的语句,均按被调函数的顺序执行
4.递归函数中位于递归调用之后的语句,均按被调函数相反的顺序执行。
5.虽然每级递归都有自己的变量,但是并没有拷贝函数的代码
程序按顺序执行函数中的代码,而递归调用就相当于又从头开始执行函数的代码。
6.递归函数必须包含能让递归调用停止的语句。
通常,递归函数都使用 if 或者其他等价的测试条件在函数形参等于某特定值时终止递归。为此,每次递归调用的形参都要使用不同的值。
尾递归的概念
最简单的递归形式是把递归调用置于函数的末尾,即正好在 return 语句之前。
递归和循环的选择
一般而言,选择循环比较好。
首先,每次递归都会创建一组变量,所以递归使用得内存更多,而且每次递归调用都会把创建的一组新变量放在栈中。递归调用的数量受限于内存空间。
其次由于每次函数调用要花费一定的时间,所以递归的执行速度较慢。
递归和倒序计算
十进制转成二进制的小程序:
//将十进制数准换成二进制数
#include <stdio.h>
void to_binary(unsigned long n);
int main(void)
{
unsigned long number;
printf("Enter an integer (q to quit):\n");
while (scanf_s("%lu",&number) == 1)
{
printf("Binary equivalent: ");
to_binary(number);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
printf("End\n");
return 0;
}
void to_binary(unsigned long n)/*递归函数*/
{
int r;
r = n % 2;
if ( n >= 2)
to_binary(n / 2);
putchar(r == 0 ? '0' : '1');
return;
}以上代码利用递归的方式将十进制转换成二进制,需要注意一点的就是十进制为偶数时末尾一定是0,十进制为奇数时末尾一定是1。
还有一点就是当与2相除的结构小于2时停止计算,因为只要结果大于或等于2,就说明还有二进制位。
边栏推荐
- 365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
- Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
- QT learning: qdropevent drag event
- 浅谈AspectJ框架
- QT系列---安装
- 365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举
- 法线可视化
- MySQL sorts the queried result set according to the specified sequence
- About the configuration and use of thymeleaf
- 阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
猜你喜欢

Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"

Functions in MySQL statements

京东云金秋上云特惠进行中!扫码参与活动

OCCT学习002-----环境搭建

C 语言手写 QQ-AI 版

容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)

MySQL的详细安装使用教程(保姆式安装图文讲解)

More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest

Deep learning brush a bunch of tricks of SOTA

About realizing page Jump of website in Servlet
随机推荐
On AspectJ framework
Soft link & hard link
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
直播预告:京东云DevOps与JFrog制品库的融合
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
MySQL many to many relationship, grouping and splicing to query multiple data to one data
Deep learning brush a bunch of tricks of SOTA
Rimworld通过SteamCMD上传创意工坊的方法
如视技术副总裁杨永林:当传统产业遇到“数字空间”
QT学习:QDropEvent拖拽事件
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
C语言连连看秒杀辅助
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
研发效能|Kubernetes核心技术剖析和DevOps落地经验
数据泄漏、删除事件频发,企业应如何构建安全防线?
6.2 function-parameters
SM integration is as simple as before, and the steps are clear (detailed)
QT学习:使用JSON/XML等非ts文件实现多语言国际化
What is_ GLIBCXX_ VISIBILITY(default)
C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)