猿问

如何使 go 程序递归

我怎么能让这个 go 程序递归。我正在通过编写游戏编号分析器来学习 golang。我一直在思考如何做到这一点,但无法提出一个可行的解决方案。这是Google Playground 中的链接。任何帮助将不胜感激。


/*

File record.go

Author: Dan Huckson

Date: 20160120

Purpose: Number analyzer

*/

package main


import (

    "fmt"

)


type Stats struct {

    category map[string]Events

}


type Events struct {

    event map[string]*Event

}


type Event struct {

    value int64

}


func main() {

    winners := [][]int{

        {1, 2, 3, 4, 5, 6},

        {2, 4, 6, 28, 26, 39},

        {1, 4, 9, 10, 26, 39},

        {1, 9, 19, 29, 26, 49},

        {4, 5, 6, 28, 26, 49}}


    keys := []string{"digits1", "digits2", "digits3", "digits4", "digits5", "digits6"}


    stats := new(Stats)

    stats.category = make(map[string]Events)


    for _, key := range keys {

        events, ok := stats.category[key]

        if !ok {

            events = *new(Events)

        }

        events.event = make(map[string]*Event)

        stats.category[key] = events


    }

    fmt.Println()


    for _, winner := range winners {

        fmt.Println(winner)

        stats.digits1("digits1", winner)

        stats.digits2("digits2", winner)

        stats.digits3("digits3", winner)

        stats.digits4("digits4", winner)

        stats.digits5("digits5", winner)

        stats.digits6("digits6", winner)

    }

}


func (stats *Stats) record(key string, balls string) {


    event, ok := stats.category[key].event[balls]

    if !ok {

        event = new(Event)

        stats.category[key].event[balls] = event

    }

    stats.category[key].event[balls].value += 1


    word := ""

    if len(balls) > 1 {

        word = "Balls"

    } else {

        word = "Ball"

    }


    fmt.Printf("%s:%s\tCount:%d\n", word, balls_to_csv(balls), stats.category[key].event[balls].value)

}


func (stats *Stats) digits1(key string, winner []int) {

    for i := 0; i < len(winner); i++ {

        stats.record(key, string(winner[i]))

    }

}


慕田峪7331174
浏览 164回答 2
2回答

慕后森

据我所知,你想递归地找到所有中奖号码的组合。例如,package mainimport "fmt"func combinations(n []int, c []int, ccc [][][]int) [][][]int {&nbsp; &nbsp; if len(n) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return ccc&nbsp; &nbsp; }&nbsp; &nbsp; if len(ccc) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; ccc = make([][][]int, len(n))&nbsp; &nbsp; }&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 = combinations(n[i+1:], cc, ccc)&nbsp; &nbsp; }&nbsp; &nbsp; return ccc}func main() {&nbsp; &nbsp; n := []int{1, 2, 3, 4}&nbsp; &nbsp; fmt.Println("winning numbers", n)&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; nw := 0&nbsp; &nbsp; w := combinations(n, nil, nil)&nbsp; &nbsp; fmt.Println("winning tickets:")&nbsp; &nbsp; d := " digit : "&nbsp; &nbsp; for i := range w {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Print(i+1, d)&nbsp; &nbsp; &nbsp; &nbsp; d = " digits: "&nbsp; &nbsp; &nbsp; &nbsp; for j := range w[i] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nw++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Print(w[i][j], " ")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; fmt.Println(nw, "winners")}输出:winning numbers [1 2 3 4]winning tickets:1 digit : [1] [2] [3] [4]&nbsp;2 digits: [1 2] [1 3] [1 4] [2 3] [2 4] [3 4]&nbsp;3 digits: [1 2 3] [1 2 4] [1 3 4] [2 3 4]&nbsp;4 digits: [1 2 3 4]&nbsp;15 winners简化一下,你可以看到递归。func combinations(n []int) {&nbsp; &nbsp; if len(n) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; for i := range n {&nbsp; &nbsp; &nbsp; &nbsp; combinations(n[i+1:])&nbsp; &nbsp; }&nbsp; &nbsp; return&nbsp;}递归在 时终止len(n) == 0。在 for 循环中,i增加到len(n)-1、combinations(n[i+1:])变成combinations(n[len(n):])、 和len(n[len(n):]) == 0,这将终止递归。

湖上湖

递归不是语言特定的概念。如果您知道什么是递归,并且知道如何在 Go 中编写函数,那么您就可以在 Go 中编写递归函数。这是 Go 中的虚拟递归函数。func f(n int) int {&nbsp; &nbsp; if n < 0 {&nbsp; &nbsp; &nbsp; &nbsp; return 0 // or something else&nbsp; &nbsp; }&nbsp; &nbsp; if n == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; }&nbsp; &nbsp; return n * f(n - 1)}这是 Go 中的递归函数,因为,它有一个终止(基本)条件(n <= 0)对于所有 x < n,f(n) 取决于 f(x)。它是用 Go 编写的。
随时随地看视频慕课网APP

相关分类

Go
我要回答