当前位置:网站首页>【愚公系列】2022年07月 Go教学课程 025-递归函数
【愚公系列】2022年07月 Go教学课程 025-递归函数
2022-08-01 03:37:00 【愚公搬代码】
一、递归函数
如果一个函数在内部调用自身本身,这个函数就是递归函数。
递归函数的优点是定义简单,逻辑清晰。理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑不如递归清晰。
构成递归的条件:
- 子问题须与原始问题为同样的事,且更为简单
- 不能无限制地调用本身,须有个出口,化简为非递归状况处理
1.递归函数的基本使用
package main
import "fmt"
func main() {
c:=Test(3)
fmt.Println(c)
}
func Test(n int) int {
// 只有第一排的人才知道自己的排数
if n == 1{
return 1
}
// 如果不是第一排,问一下前一排的人
r := Test(n-1)
fmt.Println("前一排的排数:",r)
// 把前一排人的排数+1,计算出自己的排数。
return r+1
}

3.相关案例
一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。1808年,基斯顿·卡曼引进这个表示法。
亦即n!=1×2×3×…×(n-1)×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n。
package main
var s int = 1
func main() {
TestDemo(5)
print(s)
}
func TestDemo(n int) {
if n == 1 {
return
}
s *= n
TestDemo(n - 1)
}

总结
递归就是一个函数在其内部可以调用其本身,那么这个函数就是递归函数即自己调用自己的函数
- 优点:结构清晰,可读性强,可以极大的减少代码量,用有限的语句来定义对象的无限集合。
- 缺点:效率低,调用栈可能会溢出:函数每次调用都会在内存栈中分配空间,而每个进程的容量是有限的,当调用的层次太多时,就会超出栈的容量,从而导致溢出。
边栏推荐
- Modify Postman installation path
- HCIP (14)
- Step by step hand tearing carousel Figure 3 (nanny level tutorial)
- Guys, MySQL cdc source recycles replication slave and r in incremental process
- Message queue MySQL table for storing message data
- HCIP(14)
- After specifying set 'execution.savepoint.path', restart flinksql and report this error
- 被 CSDN,伤透了心
- MySQL修改SQL语句优化性能
- leetcode6132. 使数组中所有元素都等于零(简单,周赛)
猜你喜欢
![Valentine's Day Romantic 3D Photo Wall [with source code]](/img/a9/2c26f4f048f3c0a9a65551bc734233.png)
Valentine's Day Romantic 3D Photo Wall [with source code]

The fledgling Xiao Li's 112th blog project notes: Wisdom cloud intelligent flower watering device actual combat (1) - basic Demo implementation

win10 fixed local IP

Software Testing Interview (3)

Hackers can how bad to what degree?

Simple and easy to use task queue - beanstalkd

Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.

second uncle

Completely closed Chrome updated and in the top right corner of the tip

button remove black frame
随机推荐
[Search topic] After reading the inevitable BFS solution to the shortest path problem
Input input box cursor automatically jumps to the last bug after the previous input
The kernel's handling of the device tree
Open source project site must-have & communication area function
Four implementations of
batch insert: have you really got it? Error using ts-node
Article summary: the basic model of VPN and business types
[SemiDrive source code analysis] series article link summary (full)
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
Flutter Tutorial 01 Configure the environment and run the demo program (tutorial includes source code)
The kernel of the decompression process steps
2. # code comments
[Getting Started Tutorial] Rollup Module Packager Integration
Unity在BuildIn渲染管线下实现PlanarReflection的初级方法
What is a programming language
Game Theory (Depu) and Sun Tzu's Art of War (42/100)
Simple and easy to use task queue - beanstalkd
This map drawing tool is amazing, I recommend it~~
Nmap 操作手册 - 完整版
button去除黑框