-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp89.go
97 lines (83 loc) · 1.76 KB
/
p89.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func main() {
start := time.Now()
f, _ := os.Open("./data/d89")
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
BtoI := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
//ItoB := map[int]byte{1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M'}
ans := 0
for i := 0; i < 1000; i++ {
scanner.Scan()
numStr := scanner.Text()
minStr := []byte{}
val := BtoI[numStr[len(numStr)-1]]
for i := 0; i < len(numStr)-1; i++ {
if BtoI[numStr[i]] < BtoI[numStr[i+1]] {
val -= BtoI[numStr[i]]
} else {
val += BtoI[numStr[i]]
}
}
for val >= 1000 {
minStr = append(minStr, 'M')
val -= 1000
}
for val >= 100 {
if val/100 == 9 {
minStr = append(minStr, 'C', 'M')
val -= 900
} else if val/100 >= 5 {
minStr = append(minStr, 'D')
val -= 500
} else if val/100 == 4 {
minStr = append(minStr, 'C', 'D')
val -= 400
} else {
minStr = append(minStr, 'C')
val -= 100
}
}
for val >= 10 {
if val/10 == 9 {
minStr = append(minStr, 'X', 'C')
val -= 90
} else if val/10 >= 5 {
minStr = append(minStr, 'L')
val -= 50
} else if val/10 == 4 {
minStr = append(minStr, 'X', 'L')
val -= 40
} else {
minStr = append(minStr, 'X')
val -= 10
}
}
for val > 0 {
if val == 9 {
minStr = append(minStr, 'I', 'X')
val -= 9
} else if val >= 5 {
minStr = append(minStr, 'V')
val -= 5
} else if val == 4 {
minStr = append(minStr, 'I', 'V')
val -= 4
} else {
minStr = append(minStr, 'I')
val -= 1
}
}
ans += len(numStr) - len(minStr)
}
fmt.Println(ans)
f.Close()
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}