-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0012____Integer to Roman.go
69 lines (65 loc) · 1 KB
/
0012____Integer to Roman.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
package main
// 0ms, 9
// update some condition tests for better perf.
func _0012(num int) string {
var ans []rune
for num >= 1000 {
num -= 1000
ans = append(ans, 'M')
}
if num >= 900 {
num -= 900
ans = append(ans, 'C', 'M')
}
if num >= 500 {
num -= 500
ans = append(ans, 'D')
}
if num >= 400 {
num -= 400
ans = append(ans, 'C', 'D')
}
for num >= 100 {
num -= 100
ans = append(ans, 'C')
}
if num >= 90 {
num -= 90
ans = append(ans, 'X', 'C')
}
if num >= 50 {
num -= 50
ans = append(ans, 'L')
}
if num >= 40 {
num -= 40
ans = append(ans, 'X', 'L')
}
for num >= 10 {
num -= 10
ans = append(ans, 'X')
}
if num >= 9 {
num -= 9
ans = append(ans, 'I', 'X')
}
if num >= 5 {
num -= 5
ans = append(ans, 'V')
}
if num >= 4 {
num -= 4
ans = append(ans, 'I', 'V')
}
for num != 0 {
num -= 1
ans = append(ans, 'I')
}
return string(ans)
}
// num is a positive number not exceeding 3999
func intToRoman(num int) string {
return _0012(num)
}
func main() {
}