Integer to Roman
Description:
Given an integer, convert it to a roman numeral.
The number is guaranteed to be within the range from 1 to 3999.
Example
4 -> IV
12 -> XII
21 -> XXI
99 -> XCIX
more examples at: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
Clarification
What is Roman Numeral?
https://en.wikipedia.org/wiki/Roman_numerals
https://zh.wikipedia.org/wiki/罗马数字
http://baike.baidu.com/view/42061.htm
Code:
class Solution:
"""
@param n: The integer
@return: Roman representation
"""
def intToRoman(self, n):
# write your code here
values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]
numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ]
res = ""
for i in range(len(values)):
while n>=values[i]:
res = res + numerals[i]
n -= values[i]
return res