一、题目
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
把给定的整数转换成罗马数字
二、思路
这道题其实跟13题是两个相反的过程,首先将罗马数字与整数用字典的形式存储起来,然后用给定的整数与之作比较处理。
三、代码
def intToRoman0(num):
"""
:type num: int
:rtype: str
"""
IntToChar = {1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
40: "XL",
10: "X",
9: "IX",
5: "V",
4: "IV",
1: "I",}
string = ''
for i in IntToChar.keys():
while num >= i:
num -= i
string+=IntToChar[i]
print(string)
return string
参考博客:https://blog.csdn.net/daigualu/article/details/73928733 https://blog.csdn.net/hcbbt/article/details/44026099