-
Notifications
You must be signed in to change notification settings - Fork 0
/
p.go
56 lines (46 loc) · 755 Bytes
/
p.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
)
func main() {
n := 4
buf := make([]int, n)
cur := make([]int,n)
for i,_ := range buf {
buf[i] = i
cur[i] = 0
}
for {
print(buf)
isOverflow := false
for j:=n-1;j>=0;j-- {
if cur[j] != 0 {
tmp := buf[j]
buf[j] = buf[j+cur[j]]
buf[j+cur[j]] = tmp
}
if cur[j] + 1 > n-1-j {
if j == 0 {
isOverflow = true
} else {
continue
}
} else {
cur[j] ++
tmp := buf[j]
buf[j] = buf[j+cur[j]]
buf[j+cur[j]] = tmp
break
}
}
if isOverflow {
break
}
}
}
func print(b []int) {
for _,j := range b {
fmt.Print(" ", j)
}
fmt.Println()
}