-
Notifications
You must be signed in to change notification settings - Fork 27
/
389.go
70 lines (62 loc) · 1.14 KB
/
389.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
// UVa 389 - Basically Speaking
package main
import (
"fmt"
"math"
"os"
)
func toNum(digit rune) int {
if digit >= '0' && digit <= '9' {
return int(digit - '0')
}
if digit >= 'A' && digit <= 'Z' {
return int(digit-'A') + 10
}
return -1
}
func base10(num string, base int) int {
length := len(num)
total := 0
for i, d := range num {
digit := toNum(d)
if digit >= base {
return -1
}
total += digit * int(math.Pow(float64(base), float64(length-i-1)))
}
return total
}
func toStr(d int) string {
if d <= 9 {
return string('0' + d)
}
return string('A' + d - 10)
}
func baseN(num int, base int) string {
var number string
for num > 0 {
digit := num % base
number = toStr(digit) + number
num = (num - digit) / base
}
return number
}
func main() {
in, _ := os.Open("389.in")
defer in.Close()
out, _ := os.Create("389.out")
defer out.Close()
var number string
var b1, b2 int
for {
if _, err := fmt.Fscanf(in, "%s%d%d", &number, &b1, &b2); err != nil {
break
}
bt := base10(number, b1)
if num := baseN(bt, b2); len(num) > 7 {
fmt.Fprintln(out, " ERROR")
} else {
fmt.Fprintf(out, "%7s\n", num)
}
}
}