Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02ba3d1

Browse files
author
Yeqi Tao
committed
Add Soulution.go for 0012.Inreger to Roman
1 parent caba2c5 commit 02ba3d1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 字符 数值
2+
// I 1
3+
// V 5
4+
// X 10
5+
// L 50
6+
// C 100
7+
// D 500
8+
// M 1000
9+
var table = make(map[int]string)
10+
var keys = []int{1,4,5,9,10,40,50,90,100,400,500,900,1000}
11+
12+
func init() {
13+
table[1] = "I"
14+
table[4] = "IV"
15+
table[5] = "V"
16+
table[9] = "IX"
17+
table[10] = "X"
18+
table[40] = "XL"
19+
table[50] = "L"
20+
table[90] = "XC"
21+
table[100] = "C"
22+
table[400] = "CD"
23+
table[500] = "D"
24+
table[900] = "CM"
25+
table[1000] = "M"
26+
}
27+
28+
func intToRoman(num int) string {
29+
if n, exist := table[num]; exist {
30+
return n
31+
}
32+
var result string
33+
lenKeys := len(keys)
34+
for i:=lenKeys-1; i>=0; i-- {
35+
d := keys[i]
36+
count := num/d
37+
result += genreateRoman(d, count)
38+
num = num - d * count
39+
}
40+
return result
41+
}
42+
43+
func genreateRoman(n, count int) string {
44+
var r string
45+
for i:=0; i<count; i++ {
46+
r += table[n]
47+
}
48+
return r
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /