Leetcode题库——12.整数转罗马数字


@author: ZZQ

@software: PyCharm

@file: intToRoman.py

@time: 2018/9/28 21:59

要求:

字符 数值

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

IV:4 IX: 9

XL:40 XC:90

CD:400 CM:900

class Solution():
def __init__(self):
pass def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
roma_value = ""
while num > 0:
if num >= 1000:
roma_value += 'M'
num -= 1000
if 1000 > num >= 900:
roma_value += "CM"
num -= 900
if 900 > num >= 500:
roma_value += 'D'
num -= 500
if 500 > num >= 400:
roma_value += "CD"
num -= 400
if 400 > num >= 100:
roma_value += 'C'
num -= 100
if 100 > num >= 90:
roma_value += "XC"
num -= 90
if 90 > num >= 50:
roma_value += 'L'
num -= 50
if 50 > num >= 40:
roma_value += "XL"
num -= 40
if 40 > num >= 10:
roma_value += 'X'
num -= 10
if 10 > num >= 9:
roma_value += "IX"
num -= 9
if 9 > num >= 5:
roma_value += 'V'
num -= 5
if 5 > num >= 4:
roma_value += "IV"
num -= 4
if 4 > num >= 1:
roma_value += "I"
num -= 1
return roma_value if __name__ == "__main__":
answer = Solution()
print answer.intToRoman(1994)
上一篇:2017年7月最新浏览器市场份额,IE8份额仅剩个位数


下一篇:bug-4——bootStrap中的table语言设置