-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegertoroman.py
55 lines (46 loc) · 1.14 KB
/
integertoroman.py
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
"""
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Example 3:
Input: 9
Output: "IX"
Example 4:
Input: 58
Output: "LVIII"
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Example 5:
Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
create by swm 2018/05/31
"""
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
c={0:("","I","II","III","IV","V","VI","VII","VIII","IX"),
1:("","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"),
2:("","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"),
3:("","M","MM","MMM")}
roman=[]
roman.append(c[3][num//1000%10])
roman.append(c[2][num//100%10])
roman.append(c[1][num//10%10])
roman.append(c[0][num%10])
s=''
for i in roman:
s=s+i
return s
if __name__ == '__main__':
s = Solution()
re = s.intToRoman(454)
print(re)