-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path574.go
69 lines (62 loc) · 1.19 KB
/
574.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
69
// UVa 574 - Sum It Up
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
var (
t int
nums []int
answers [][]string
)
func sameAsLast(selected []string) bool {
if len(answers) == 0 {
return false
}
for i := range selected {
if selected[i] != answers[len(answers)-1][i] {
return false
}
}
return true
}
func backtracking(sum, idx int, selected []string) {
if sum == t {
if !sameAsLast(selected) {
answers = append(answers, selected)
}
return
}
if idx == len(nums) || sum > t || len(selected) == len(nums) {
return
}
backtracking(sum+nums[idx], idx+1, append(selected, strconv.Itoa(nums[idx])))
backtracking(sum, idx+1, selected)
}
func main() {
in, _ := os.Open("574.in")
defer in.Close()
out, _ := os.Create("574.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d%d", &t, &n); t == 0 && n == 0 {
break
}
nums = make([]int, n)
for i := range nums {
fmt.Fscanf(in, "%d", &nums[i])
}
answers = nil
backtracking(0, 0, nil)
if fmt.Fprintf(out, "Sums of %d:\n", t); len(answers) == 0 {
fmt.Fprintln(out, "NONE")
} else {
for _, answer := range answers {
fmt.Fprintln(out, strings.Join(answer, "+"))
}
}
}
}