题目来源:
https://leetcode.com/problems/integer-to-roman/
题意分析:
这道题是要把在区间[1-3999]的数字转化成罗马数字。
题目思路:
只要知道了罗马数字和阿拉伯数字是怎么转换的就不难了,要注意的是900,500,400,90,50,40,9,5,4分别应该是‘CM’,‘D’,‘CD’,‘XC’,‘L’,‘XL’,‘IX’,‘V’,‘IV’。
代码(python):
1 class Solution(object): 2 def intToRoman(self, num): 3 """ 4 :type num: int 5 :rtype: str 6 """ 7 a = [1000,900,500,400,100,90,50,40,10,9,5,4,1] 8 b = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'] 9 ans = '' 10 i = 0 11 count = 0 12 while num > 0: 13 count = num/a[i] 14 num %= a[i] 15 while count > 0: 16 ans += b[i] 17 count -= 1 18 i += 1 19 return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/4817819.html