Recursion, as the name suggests, is calling itself , I studied by myself python I wrote some small cases when I was , I think it's interesting , Take it out and write it again today !
notes : The recursion cases are all classic cases
Record your study for the author , If there's something wrong , Welcome to advise !
Personally, I think recursion only needs to find three elements , Clear logic , Can write a complete recursive function !
Here are three examples to illustrate recursion : example 1
The user enters a number himself , Find the factorial !Golang Language implementation
// @author: Have a bottle of anmuxi
// @time:2020-10-21 15:10:34
// @file: recursive .go
// @ Start a good day ('μ')
package main
import "fmt"
// Calculation n The factorial
func f(n uint64)uint64{
if n<=1{
return 1
}
return n * f(n-1)
}
func main() {
var n uint64
fmt.Print(" Please enter the order multiplier you want to enter :")
fmt.Scan(&n)
ret := f(n)
fmt.Printf("%d The factorial of is %v",n,ret)
python Language implementation
# -*- coding = utf-8 -*-
# @Time:2020-10-21 16:00
# @Author: Have a bottle of anmuxi
# @File: recursive .py
# @ Start a good day @[email protected]
def jiecheng(n):
if n==1:
return 1
return n*jiecheng(n-1)
n = input(" Please enter the order multiplier you want to calculate :")
print(jiecheng(int(n)))
example 2
Fibonacci sequence
Fibonacci series Baidu Encyclopedia definition ( link )
The Fibonacci sequence refers to a sequence that looks like this :0,1,1,2,3,5,8,13,21,34,…
This sequence starts at number one 3 A start , Each of these terms is equal to the sum of the first two terms .Golang Language
package main
import "fmt"
func feibo(n int)int {
if n==1{
return 0
}
if n==2{
return 1
}
return feibo(n-1)+feibo(n-2)
}
func main() {
fmt.Println(feibo(10)) // The answer is 34
}
Python Language
def feibonaqi(n):
if n==1:
return 0
if n==2:
return 1
return feibonaqi(n-1)+feibonaqi(n-2)
print(feibonaqi(10)) # The answer is 34
example 3
Climb the stairs : There is a staircase for n level , At first you were at the first level , If you can only step up one or two levels at a time , To go on n level , How many ways are there ?
analysis : No matter how you go ahead , When one comes to the end , Faced with two situations :1. There is only one step , So there's only one way to go . 2. Two steps left , Then there are two other ways
ok! So as long as we use recursive calculation to calculate the previous method when there is only one step left + When there are only two steps left, the first step is return climbStep(n-1)+climbStep(n-2)
. With 5 For example , Calculate how many ways there are .
1 1 1 1 1
1 1 1 2
1 1 2 1
1 2 1 1
2 1 1 1
1 2 2
2 1 2
2 2 1
All in all 8 Seed walking method !
Golang Language implementation
package main
import "fmt"
// Go up the steps : There is a staircase for n level , At first you were at the first level , If you can only step up one or two levels at a time , To go on n level , How many ways are there ?
func climblader(n int)int{
if n ==1 {
return 1
}
if n==2 {
return 2
}
return climblader(n-1)+climblader(n-2)
}
func main() {
ret := climblader(6)
fmt.Println(ret)
}
Python
def climbStep(n):
if n==1:
return 1
elif n==2:
return 2
return climbStep(n-1)+climbStep(n-2)
print(climbStep(5))