码迷,mamicode.com
首页 > 其他好文 > 详细

golang之递归

时间:2017-12-30 19:04:00      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:代码   actor   golang   style   int   gpo   ++   blog   color   

翠花,上代码

package main

import (
    "fmt"
    "time"
)

/*
递归原则,一个大问题分解成相似的小问题
定义好出口条件,否则死循环
*/
func calc(n int) int {
    if n == 1 {
        return 1
    }
    return calc(n-1) * n
}

func recusive(n int) {
    fmt.Println("你好呀")
    time.Sleep(time.Second)
    if n > 10 {
        return
    }
    recusive(n + 1)
}
func factor(n int) int {
    if n == 1 {
        return 1
    }
    return factor(n-1) * n
}

//斐波那
func fab(n int) int {
    if n <= 1 {
        return 1
    }
    return fab(n-1) + fab(n-2)
}

func main() {
    //fmt.Println(factor(5))
    //recusive(0)
    for i := 0; i < 10; i++ {
        fmt.Println(fab(i))
    }
}

 

golang之递归

标签:代码   actor   golang   style   int   gpo   ++   blog   color   

原文地址:https://www.cnblogs.com/pyyu/p/8150555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!