This commit is contained in:
2026-05-29 14:59:42 +08:00
parent 75abd5b27a
commit ec1fd8e96a
2 changed files with 35 additions and 0 deletions
@@ -0,0 +1 @@
{"name":"Local: 4","url":"/home/wonder/code/algrithm/LC1/4.go","tests":[{"id":1780037619072,"input":"5 1","output":"1 \n2 \n3 \n4 \n5 \n"},{"id":1780037766692,"input":"5 3","output":"1 2 3\n1 2 4\n1 2 5\n1 3 4\n1 3 5\n1 4 5\n2 3 4\n2 3 5\n2 4 5\n3 4 5"},{"id":1780037945579,"input":"6 2","output":"1 2 \n1 3 \n1 4 \n1 5 \n1 6 \n2 3 \n2 4 \n2 5 \n2 6 \n3 4 \n3 5 \n3 6 \n4 5 \n4 6 \n5 6 \n"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/home/wonder/code/algrithm/LC1/4.go","group":"local","local":true}
+34
View File
@@ -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)
}