Go study notes - recursion (factorial, Fibonacci sequence, Tower of Hanoi problem)

Go study notes - recursion


If you have any questions, please leave a message to correct me! Thanks for watching


1. Definition of recursion

A program calls itself during execution.

Usually in a .gosingle file, a function is used to achieve a cyclic call.

Conditions that constitute recursion:

  1. A subproblem is the same thing as the original problem, but simpler.
  2. There must be an exit for recursive calls, which is the condition for the end of the recursive function.

The use of recursion is generally regular.

2. Example of recursion

1. Factorial case

The factorial of 9 can be simplified as 9!=9x8x7x6x5x4x3x2x1x1, first the factorial of 0 is 1, so the condition for the end of factorial recursion is

//阶乘递归结束的条件,f(0)=1
if n==0 {
    
    
    return 1
}
  • n==1, the mathematical formula is1*f(1-1)
  • n==2, the mathematical formula is2*f(2-1)*f(1-1)
  • So for the nth item, the mathematical formula is n*f(n-1)*f(n-1-1)*...*f(n-1-1-...-1)=n*f(n-1)*f(n-2)*...*f(1)*f(0)
func fact(n int) int{
    
    
    if n==0{
    
    
        return 1
    }
    return n*fact(n-1)
}

//7!=5040

2. FibonacciSequence

The form of the Fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

  • Mathematically expressed as: f(0)=0, f(1)=1, f(2)=f(2-1)+f(2-2),…
  • The general term formula is: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2)

The condition for the end of recursion is F(0)=0, F(1)=1

//斐波那契数列递归结束的条件
if n==0{
    
    
    return 0
}else if n==1{
    
    
    return 1
}

The full function using recursion is:

func fibonacci(n int) int{
    
    
    if n==0{
    
    
        return 0
    }else if n==1{
    
    
        return 1
    }
    return fibonacci(n-1)+fibonacci(n-2)
}

//3 = 2

3. Tower of Hanoi ( Hanoi) problem

The Tower of Hanoi problem is to move some plates from A-pillar to C-pillar. During the moving process, ensure that the large plate is always under the small plate. During the moving process, all the plates in A-pillar can be moved to C-pillar with the help of B-pillar.

Disassembly steps:

  • When there is only one plate in column A, move the plate directly from column A to column C. This is the condition for the end of the recursion

    //汉诺塔问题递归结束的条件
    if n==1{
          
          
        hanoiMove(A,C)
        //从A->C
    }
    
  • When the A column has two plates, first A->B, then A->C, then B->C

The full function using recursion is:

//定义全局变量,获取移动盘子的次数
var count int
//定义移动函数,并统计移动次数
func hanoiMove(a,b string){
    
    
    count ++
    fmt.Printf("%s->%s",a,b)
}
//定义递归函数,判断移动的顺序
func hanoi(n int,a,b,c string){
    
    
    if n==1{
    
    
        hanoiMove(a,c)
    }else{
    
    
        hanoi(n-1,a,c,b) //把A移到B
        hanoiMove(a,c)
        hanoi(n-1,b,a,c) //把B移到C
    }
}


func main(){
    
    
    a,b,c := "A","B","C"
    hanoi(2,a,b,c)
    fmt.Println(count)
}

//A->B
//A->C
//B->C
//3

Guess you like

Origin blog.csdn.net/weixin_46435420/article/details/119411637