当前位置:网站首页>递归的基本原理
递归的基本原理
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,就说明还有二进制位。
边栏推荐
- Teardown 解除时间限制的方法
- 京东云金秋上云特惠进行中!扫码参与活动
- 研发效能生态完整图谱&DevOps工具选型必看
- 数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
- scikit-learn——机器学习应用开发的步骤和理解
- Getting started with solidity
- NVIDIA Zhou Xijian: the last mile from design to digital marketing
- 321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
- 2022年泰迪杯数据挖掘挑战赛C题方案及赛后总结
- 研发效能|Kubernetes核心技术剖析和DevOps落地经验
猜你喜欢

AI应用第一课:C语言支付宝刷脸登录

Xiaobai high salary shortcut Qt development game Snake

About realizing page Jump of website in Servlet

Handwritten student management system

C language handwritten qq-ai version

6.2 function-parameters

The latest tank battle 2022 - Notes on the whole development -2

英伟达周锡健:设计到数字营销的最后一公里

Young freshmen yearn for more open source | here comes the escape guide from open source to employment!

365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
随机推荐
C how to realize simple factory mode
小白高薪捷径-Qt开发游戏—贪吃蛇
Rolabelimg to data format data
Visual Basic .Net 如何获取命令参数
平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
OCCT学习002-----环境搭建
直播预告|如何通过“智能边缘安全”提升企业免疫力?
ARFoundation入门教程10-平面检测和放置
200 多家 ISV 入驻!阿里云计算巢发布一周年
How mongodb inserts, deletes and updates documents
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
7.3-function-templates
AI应用第一课:C语言支付宝刷脸登录
JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
About the configuration and use of thymeleaf
预约中,2022京东云产业融合新品发布会线上开启
QT series - Installation
研发效能|Kubernetes核心技术剖析和DevOps落地经验
Architecture analysis of three-tier project and parameter name injection of construction method