update
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
//go:build ignore
|
||||
|
||||
// 组合的输出 从 n 个元素(1-n)中选取 r 个数
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var (
|
||||
n int
|
||||
r int
|
||||
path []int
|
||||
)
|
||||
|
||||
func dfs(x int) {
|
||||
// Return
|
||||
if len(path) >= r {
|
||||
for _, v := range path {
|
||||
fmt.Printf("%d ", v)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
return
|
||||
}
|
||||
// Traverse
|
||||
for i := x; i <= n; i++ {
|
||||
path = append(path, i)
|
||||
dfs(i + 1)
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Scan(&n, &r)
|
||||
dfs(1)
|
||||
}
|
||||
Reference in New Issue
Block a user