-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path725.go
73 lines (66 loc) · 1.1 KB
/
725.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
70
71
72
73
// UVa 725 - Division
package main
import (
"fmt"
"os"
)
func unique(n int) (bool, map[int]bool) {
m := make(map[int]bool)
for i := 0; i < 5; i++ {
r := n % 10
if m[r] {
return false, nil
}
m[r] = true
n = n / 10
}
return true, m
}
func notIn(n int, m map[int]bool) bool {
for i := 0; i < 5; i++ {
r := n % 10
if m[r] {
return false
}
n = n / 10
}
return true
}
func solve(n int) [][2]int {
var ans [][2]int
for i := 10234; i <= 98765; i++ {
if ok, m := unique(i); ok {
d := i / n
if d*n == i {
if ok, _ := unique(d); ok {
if notIn(d, m) {
ans = append(ans, [2]int{i, d})
}
}
}
}
}
return ans
}
func main() {
in, _ := os.Open("725.in")
defer in.Close()
out, _ := os.Create("725.out")
defer out.Close()
var n int
for kase := 1; ; kase++ {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
if kase > 1 {
fmt.Fprintln(out)
}
if ans := solve(n); len(ans) == 0 {
fmt.Fprintf(out, "There are no solutions for %d.\n", n)
} else {
for _, v := range ans {
fmt.Fprintf(out, "%d / %05d = %d\n", v[0], v[1], n)
}
}
}
}