-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10077.go
47 lines (39 loc) · 818 Bytes
/
10077.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
// UVa 10077 - The Stern-Brocot Number System
package main
import (
"fmt"
"os"
)
type node struct{ m, n int }
func (n1 node) lessThan(n2 node) bool { return n1.m*n2.n < n2.m*n1.n }
func (n1 node) equals(n2 node) bool { return n1.m == n2.m && n1.n == n2.n }
func binarySearch(n node) string {
var path []byte
l, r := node{0, 1}, node{1, 0}
for {
m := node{l.m + r.m, l.n + r.n}
switch {
case m.equals(n):
return string(path)
case n.lessThan(m):
path = append(path, 'L')
r = m
default:
path = append(path, 'R')
l = m
}
}
}
func main() {
in, _ := os.Open("10077.in")
defer in.Close()
out, _ := os.Create("10077.out")
defer out.Close()
var m, n int
for {
if fmt.Fscanf(in, "%d%d", &m, &n); m == 1 && n == 1 {
break
}
fmt.Fprintln(out, binarySearch(node{m, n}))
}
}