当前位置:网站首页>"Recursion" recursion concept and typical examples
"Recursion" recursion concept and typical examples
2022-08-05 05:03:00 【starry Lu Li】
活动地址:CSDN21天学习挑战赛
作者简介:一位喜欢写作,计科专业大二菜鸟
个人主页:starry陆离
首发日期:2022年8月2日星期二
上期文章:『算法导论』什么是算法?什么是程序?
订阅专栏:『算法分析与设计』
每日推荐:『牛客网-面试神器』
如果文章有帮到你的话记得点赞+收藏支持一下哦

『递归』Recursive concepts and typical examples
1.引言
问题:1-100求和
方法1:使用循环求和 1+2+3+4+5+6+……+99+100
伪代码:
for i=1 to 100
sum = sum + i
方法2:换个角度思考
sum(n)表示1…n的和
sum(100) = sum(99) + 100
= sum(98) + 99 + 100
= ……
= sum(1) + 2 + 3 + 4 + …… + 99 + 100
= 1 + 2 + 3 + 4 +…… + 99 + 100
2.递归的定义
In the process of a function called againCall this function directly or indirectly by itself,Referred to as a function of parcels 归(Recursion)调用,这种函数称为递归函数
若pFunction definition callp函数,称之为直接递归
若pFunction definition callq函数,而qFunction definition and callp函数,称之为间接递归
3.递归的要素
1、递归表达式 (递归方程)
2、递归结束条件 (边界条件)

int sum(int n){
if(n==1)return 1;//递归结束条件
else sum(n-1)+n;//递归表达式
}
4.递归特点
(1) 需要解决的问题Can be converted into one or more child problems to solve,And these subproblems 求解方法The same with the original problem,只是在数量规模上不同
(2) The number of recursive calls must be有限的
(3) 必须End of recursion condition来终止递归
5.Scope of application of recursive
(1) 定义是递归的(阶乘、斐波那契数列等)
(2) 数据结构是递归的(单链表、二叉树等)
(3) 问题的求解方法是递归的(汉诺塔、回溯法等)
6.递归的优缺点
优点:结构清晰,可读性强,而且容易用数学归纳法来证明算法的正确性, 因此它为设计算法、调试程序带来很大方便
缺点:递归算法的运行效率较低,Both cost calculation time and take up the storage of empty More than the non-recursive algorithm between
解决方法:In some of the recursive algorithm can eliminate the recursive call,使其Into non-recursive algorithm
7.Typical case of recursive
7.1求阶乘
n!=1×2×3×……×n

int f(int n){
if(n==1)return 1;//递归结束条件
else n*f(n-1);//递归表达式
}
7.2Fibonacci数列
斐波那契(Fibonacci)Sequence for mathematicians leonardo·斐波那 Qi in the rabbit breeding, for example to introduce,故又称为**“兔子数列”**.


int fibonacci(int n){
if(n<=1)return 1;//递归结束条件
return fibonacci(n-1)+fibonacci(n-2);//递归表达式
}
7.3青蛙跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级.
- 求该青蛙跳上一个n级的台阶总共有多少种跳法?
就是fibonacci的变形题,读者可自行实现
如果文章有帮到你的话记得点赞+收藏支持一下哦
边栏推荐
- Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource
- Dephi逆向工具Dede导出函数名MAP导入到IDA中
- 【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
- 雷克萨斯lm的安全性到底体现在哪里?一起来看看吧
- 电话溥功能
- Flutter Learning 4 - Basic UI Components
- Flutter TapGestureRecognizer 如何工作
- 【学习笔记之菜Dog学C】动态内存管理之经典笔试题
- Use IDEA to connect to TDengine server
- 二叉树基本性质+oj题解析
猜你喜欢

C++ core programming

Detailed explanation of Mysql's undo log

u-boot debugging and positioning means

雷克萨斯lm的安全性到底体现在哪里?一起来看看吧

upload上传图片到腾讯云,如何上传图片

Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource

How to solve complex distribution and ledger problems?

Mini Program_Dynamic setting of tabBar theme skin

The log causes these pits in the thread block, you have to guard against
![[Geek Challenge 2019]FinalSQL](/img/e4/0c8225ef7c5e7e5bdbaac2ef6fc867.png)
[Geek Challenge 2019]FinalSQL
随机推荐
C language - vernacular to understand the original code, inverse code and complement code
【cesium】3D Tileset 模型加载并与模型树关联
类的底层机制
Machine Learning Overview
A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?
服务器磁盘阵列
Analyses the mainstream across technology solutions
Excel画图
请写出SparkSQL语句
[MRCTF2020] Ezpop (detailed)
结构光三维重建(一)条纹结构光三维重建
University Physics---Particle Kinematics
8.04 Day35-----MVC three-tier architecture
Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
浅析主流跨端技术方案
Day14 jenkins deployment
特征预处理
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
Excel Paint
小程序_动态设置tabBar主题皮肤
