-
Notifications
You must be signed in to change notification settings - Fork 27
/
441.go
68 lines (61 loc) · 1.03 KB
/
441.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// UVa 441 - Lotto
package main
import (
"fmt"
"io"
"os"
"strconv"
"strings"
)
var out io.WriteCloser
func output(nums []int, selected []bool) {
var tokens []string
for i, v := range selected {
if v {
tokens = append(tokens, strconv.Itoa(nums[i]))
}
}
fmt.Fprintln(out, strings.Join(tokens, " "))
}
func backtracking(nums []int, selected []bool) {
cnt, largest := 0, -1
for i, v := range selected {
if v {
cnt++
largest = i
}
}
if cnt == 6 {
output(nums, selected)
return
}
for i := range nums {
if !selected[i] && i > largest {
selected[i] = true
backtracking(nums, selected)
selected[i] = false
}
}
}
func main() {
in, _ := os.Open("441.in")
defer in.Close()
out, _ = os.Create("441.out")
defer out.Close()
var k int
for first := true; ; {
if fmt.Fscanf(in, "%d", &k); k == 0 {
break
}
if first {
first = false
} else {
fmt.Fprintln(out)
}
nums := make([]int, k)
for i := range nums {
fmt.Fscanf(in, "%d", &nums[i])
}
backtracking(nums, make([]bool, k))
}
}