update
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,39 @@
|
|||||||
|
//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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user