golang中创建数字组合的递归函数

我想弄清楚这个递归函数的事情。我有一个有效的非递归演示,但它使用非递归的静态方法。这些函数从“pool_size”中打印出“数字集”的所有组合。如果有人可以,请帮助我使这个函数递归,那会很棒。


package main


import (

    "fmt"

)


func combos_of1(pool_size int) {

    for i := 1; i < pool_size+1; i++ {

        fmt.Println(i)

    }

    fmt.Println("\n")

}


func combos_of2(pool_size int) {

    for i := 1; i < pool_size+1; i++ {

        for j := i + 1; j < pool_size+1; j++ {

            fmt.Println(i, j)

        }

    }

    fmt.Println("\n")

}


func combos_of3(pool_size int) {

    for i := 1; i < pool_size+1; i++ {

        for j := i + 1; j < pool_size+1; j++ {

            for k := j + 1; k < pool_size+1; k++ {

                fmt.Println(i, j, k)

            }

        }

    }

    fmt.Println("\n")

}


func main() {

    combos_of1(10)

    combos_of2(10)

    combos_of3(10)

}


潇潇雨雨
浏览 158回答 1
1回答

慕工程0101907

例如,package mainimport "fmt"func rCombinations(p int, n []int, c []int, ccc [][][]int) [][][]int {&nbsp; &nbsp; if len(n) == 0 || p <= 0 {&nbsp; &nbsp; &nbsp; &nbsp; return ccc&nbsp; &nbsp; }&nbsp; &nbsp; if len(ccc) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; ccc = make([][][]int, p)&nbsp; &nbsp; }&nbsp; &nbsp; p--&nbsp; &nbsp; for i := range n {&nbsp; &nbsp; &nbsp; &nbsp; cc := make([]int, len(c)+1)&nbsp; &nbsp; &nbsp; &nbsp; copy(cc, c)&nbsp; &nbsp; &nbsp; &nbsp; cc[len(cc)-1] = n[i]&nbsp; &nbsp; &nbsp; &nbsp; ccc[len(cc)-1] = append(ccc[len(cc)-1], cc)&nbsp; &nbsp; &nbsp; &nbsp; ccc = rCombinations(p, n[i+1:], cc, ccc)&nbsp; &nbsp; }&nbsp; &nbsp; return ccc}func Combinations(p int, n []int) [][][]int {&nbsp; &nbsp; return rCombinations(p, n, nil, nil)}func main() {&nbsp; &nbsp; pools := 3&nbsp; &nbsp; numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}&nbsp; &nbsp; fmt.Println(pools, "pools", "for", "numbers", numbers)&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; nc := 0&nbsp; &nbsp; c := Combinations(pools, numbers)&nbsp; &nbsp; fmt.Println("pools:")&nbsp; &nbsp; d := " digit : "&nbsp; &nbsp; for i := range c {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(i+1, d)&nbsp; &nbsp; &nbsp; &nbsp; d = " digits: "&nbsp; &nbsp; &nbsp; &nbsp; for j := range c[i] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nc++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(c[i][j], " ")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; fmt.Println(nc, "combinations")}输出:3 pools for numbers [1 2 3 4 5 6 7 8 9 10]pools:1&nbsp; digit :&nbsp;[1]&nbsp;&nbsp;[2]&nbsp;&nbsp;[3]&nbsp;&nbsp;[4]&nbsp;&nbsp;[5]&nbsp;&nbsp;[6]&nbsp;&nbsp;[7]&nbsp;&nbsp;[8]&nbsp;&nbsp;[9]&nbsp;&nbsp;[10]&nbsp;&nbsp;2&nbsp; digits:&nbsp;[1 2]&nbsp;&nbsp;[1 3]&nbsp;&nbsp;[1 4]&nbsp;&nbsp;[1 5]&nbsp;&nbsp;[1 6]&nbsp;&nbsp;[1 7]&nbsp;&nbsp;[1 8]&nbsp;&nbsp;[1 9]&nbsp;&nbsp;[1 10]&nbsp;&nbsp;[2 3]&nbsp;&nbsp;[2 4]&nbsp;&nbsp;[2 5]&nbsp;&nbsp;[2 6]&nbsp;&nbsp;[2 7]&nbsp;&nbsp;[2 8]&nbsp;&nbsp;[2 9]&nbsp;&nbsp;[2 10]&nbsp;&nbsp;[3 4]&nbsp;&nbsp;[3 5]&nbsp;&nbsp;[3 6]&nbsp;&nbsp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sp;&nbsp;[4 5 6]&nbsp;&nbsp;[4 5 7]&nbsp;&nbsp;[4 5 8]&nbsp;&nbsp;[4 5 9]&nbsp;&nbsp;[4 5 10]&nbsp;&nbsp;[4 6 7]&nbsp;&nbsp;[4 6 8]&nbsp;&nbsp;[4 6 9]&nbsp;&nbsp;[4 6 10]&nbsp;&nbsp;[4 7 8]&nbsp;&nbsp;[4 7 9]&nbsp;&nbsp;[4 7 10]&nbsp;&nbsp;[4 8 9]&nbsp;&nbsp;[4 8 10]&nbsp;&nbsp;[4 9 10]&nbsp;&nbsp;[5 6 7]&nbsp;&nbsp;[5 6 8]&nbsp;&nbsp;[5 6 9]&nbsp;&nbsp;[5 6 10]&nbsp;&nbsp;[5 7 8]&nbsp;&nbsp;[5 7 9]&nbsp;&nbsp;[5 7 10]&nbsp;&nbsp;[5 8 9]&nbsp;&nbsp;[5 8 10]&nbsp;&nbsp;[5 9 10]&nbsp;&nbsp;[6 7 8]&nbsp;&nbsp;[6 7 9]&nbsp;&nbsp;[6 7 10]&nbsp;&nbsp;[6 8 9]&nbsp;&nbsp;[6 8 10]&nbsp;&nbsp;[6 9 10]&nbsp;&nbsp;[7 8 9]&nbsp;&nbsp;[7 8 10]&nbsp;&nbsp;[7 9 10]&nbsp;&nbsp;[8 9 10]&nbsp;&nbsp;175 combinations单个池的变化是:package mainimport "fmt"func rPool(p int, n []int, c []int, cc [][]int) [][]int {&nbsp; &nbsp; if len(n) == 0 || p <= 0 {&nbsp; &nbsp; &nbsp; &nbsp; return cc&nbsp; &nbsp; }&nbsp; &nbsp; p--&nbsp; &nbsp; for i := range n {&nbsp; &nbsp; &nbsp; &nbsp; r := make([]int, len(c)+1)&nbsp; &nbsp; &nbsp; &nbsp; copy(r, c)&nbsp; &nbsp; &nbsp; &nbsp; r[len(r)-1] = n[i]&nbsp; &nbsp; &nbsp; &nbsp; if p == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cc = append(cc, r)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; cc = rPool(p, n[i+1:], r, cc)&nbsp; &nbsp; }&nbsp; &nbsp; return cc}func Pool(p int, n []int) [][]int {&nbsp; &nbsp; return rPool(p, n, nil, nil)}func main() {&nbsp; &nbsp; pool := 9&nbsp; &nbsp; numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}&nbsp; &nbsp; p := Pool(pool, numbers)&nbsp; &nbsp; fmt.Println(pool, "digit pool", "for", "numbers", numbers)&nbsp; &nbsp; for i := range p {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(p[i])&nbsp; &nbsp; }}输出:9 digit pool for numbers [1 2 3 4 5 6 7 8 9 10][1 2 3 4 5 6 7 8 9][1 2 3 4 5 6 7 8 10][1 2 3 4 5 6 7 9 10][1 2 3 4 5 6 8 9 10][1 2 3 4 5 7 8 9 10][1 2 3 4 6 7 8 9 10][1 2 3 5 6 7 8 9 10][1 2 4 5 6 7 8 9 10][1 3 4 5 6 7 8 9 10][2 3 4 5 6 7 8 9 10]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go