From e142f1f224c44d095b2c20e84fd779f3c176ac82 Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Fri, 24 Sep 2021 01:04:44 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E9=80=92=E5=BD=92=20=E4=B8=8A=E5=8F=B0?= =?UTF-8?q?=E9=98=B6=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/function/recursion/main.go | 35 +++++++++++++++++++++++++++++++++ base/function/type/main.go | 1 - 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 base/function/recursion/main.go diff --git a/base/function/recursion/main.go b/base/function/recursion/main.go new file mode 100644 index 0000000..9b23580 --- /dev/null +++ b/base/function/recursion/main.go @@ -0,0 +1,35 @@ +package main + +import "fmt" + +// 递归 +// 计算 n 的阶乘 n! +// 3! = 3 * 2 * 1 +// 4! = 4 * 3 * 2 * 1 +// 5! = 5 * 4 * 3 * 2 * 1 +func factorial(n int) int { + if n > 0 { + return n * factorial(n-1) + } + return 1 +} + +// 上台阶 +// n个台阶 一次走 1 或 2 步, 共 多少种走法 +func computed(n uint64) uint64 { + if n == 1 { + return 1 + } + if n == 2 { + return 2 + } + return computed(n-1) + computed(n-2) +} + +func main() { + n := 5 + fmt.Printf("%d 的阶乘为 : %d\n", n, factorial(n)) + + n = 4 + fmt.Printf("%d 个台阶 共有 : %d种走法\n", n, computed(uint64(n))) +} diff --git a/base/function/type/main.go b/base/function/type/main.go index 8996e0c..2f983b8 100644 --- a/base/function/type/main.go +++ b/base/function/type/main.go @@ -48,5 +48,4 @@ func main() { mf3(f4(2, 2)) fmt.Printf("f4: %T\n", f4) - }