update
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
//go:build ignore
|
||||
|
||||
// 指数型枚举:从 1-n 这 n 个整数中随机选取任意多个
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var path []int
|
||||
|
||||
func dfs(x int, n int) { // x 是当前枚举到的位置
|
||||
if x > n {
|
||||
for i, v := range path {
|
||||
if i > 0 {
|
||||
fmt.Printf(" ")
|
||||
}
|
||||
fmt.Printf("%d", v)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
return
|
||||
}
|
||||
|
||||
// 不选
|
||||
dfs(x+1, n)
|
||||
|
||||
// 选
|
||||
path = append(path, x)
|
||||
dfs(x+1, n)
|
||||
|
||||
path = path[:len(path)-1]
|
||||
}
|
||||
|
||||
func main() {
|
||||
var n int
|
||||
fmt.Scan(&n)
|
||||
dfs(1, n)
|
||||
}
|
||||
Reference in New Issue
Block a user