-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.integer-to-roman.go
More file actions
76 lines (75 loc) · 946 Bytes
/
12.integer-to-roman.go
File metadata and controls
76 lines (75 loc) · 946 Bytes
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
/*
* @lc app=leetcode id=12 lang=golang
*
* [12] Integer to Roman
*/
func intToRoman(num int) string {
// "I": 1,
// "IV":4,
// "V": 5,
// "IX":9,
// "X": 10,
// "XL":40,
// "L": 50,
// "XC":90,
// "C": 100,
// "CD":400,
// "D": 500,
// "CM":900,
// "M": 1000,
// num 循环减去可能的最大值,-1000,-900,-500...
str := ""
for num >=1000{
str += "M"
num -= 1000
}
for num >=900{
str += "CM"
num -= 900
}
for num >=500{
str += "D"
num -= 500
}
for num >=400{
str += "CD"
num -= 400
}
for num >=100{
str += "C"
num -= 100
}
for num >=90{
str += "XC"
num -= 90
}
for num >=50{
str += "L"
num -= 50
}
for num >=40{
str += "XL"
num -= 40
}
for num >=10{
str += "X"
num -= 10
}
for num >=9{
str += "IX"
num -= 9
}
for num >=5{
str += "V"
num -= 5
}
for num >=4{
str += "IV"
num -= 4
}
for num >=1{
str += "I"
num -= 1
}
return str
}