Files

40 lines
471 B
Go
Raw Permalink Normal View History

2026-05-29 14:32:49 +08:00
//go:build ignore
// 输出自然数 1-n 的全排列
package main
import "fmt"
var (
n int
path []int
used []bool
)
func dfs() {
if len(path) == n {
for _, v := range path {
fmt.Printf("%d ", v)
}
fmt.Printf("\n")
return
}
for i := 1; i <= n; i++ {
if used[i] {
continue
}
used[i] = true
path = append(path, i)
dfs()
path = path[:len(path)-1]
used[i] = false
}
}
func main() {
fmt.Scan(&n)
used = make([]bool, n+1)
dfs()
}