当前位置:网站首页>递归的基本原理
递归的基本原理
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,就说明还有二进制位。
边栏推荐
- Numpy Foundation
- MySQL的详细安装使用教程(保姆式安装图文讲解)
- This article takes you to understand the implementation of surround notification @around and final notification @after
- JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
- 法线可视化
- WDDM learning
- Getting started with solidity
- Getting started with arfoundation tutorial 10- plane detection and placement
- CMU15-213 Malloc Lab实验记录
- SQL log
猜你喜欢

SM整合原来这么简单,步骤清晰(详细)

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

51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭

ARFoundation从零开始9-AR锚点(AR Anchor)

Getting started with arfoundation tutorial 10- plane detection and placement

C语言连连看秒杀辅助

Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team

数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录

三层项目的架构分析及构造方法的参数名称注入

This article takes you to understand the implementation of surround notification @around and final notification @after
随机推荐
来!看排名一年上升16位的ClickHouse,如何在京东落地实践
Getting started with solidity
C语言宏#define命令练习
QtCreator+CMake编译器设置
Qml类型:MouseArea
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories
ARFoundation入门教程10-平面检测和放置
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
源码编译pytorch坑
During the appointment, the 2022 JD cloud industrial integration new product launch was launched online
Helm chart for Kubernetes
JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
Visual Basic .Net 如何获取命令参数
Teardown's method of lifting the time limit
三层项目的架构分析及构造方法的参数名称注入
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
为啥谷歌的内部工具不适合你?
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
[from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]
Database course design of online assistant teaching platform for high school chemistry