Golang 遞迴列印楊輝三角

DukeAnn發表於2019-07-17
package main

import "fmt"

func main() {
    YangHuiTriangle(10)
}

func YangHuiTriangle(n int) []int {
    i := n - 1
    line := make([]int, n)
    if i <= 0 {
        line = []int{1}
        fmt.Println(line)
        return line
    }
    down := YangHuiTriangle(i)
    for x := 0; x < n; x++ {
        if x == 0 {
            line[0] = 1
            continue
        }
        if x == n-1 {
            line[x] = 1
            continue
        }
        line[x] = down[x-1] + down[x]
    }
    fmt.Println(line)
    return line
}

結果:

Golang 遞迴列印楊輝三角

如有更好方法請指教

做自己

相關文章