首页 > 其他 > 详细

12. Integer to Roman 整数转罗马数字

时间:2018-02-12 23:28:07      阅读:279      评论:0      收藏:0      [点我收藏+]

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


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
class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        I = [‘‘, ‘I‘, ‘II‘, ‘III‘, ‘IV‘, ‘V‘, ‘VI‘, ‘VII‘, ‘VIII‘, ‘IX‘]
        X = [‘‘, ‘X‘, ‘XX‘, ‘XXX‘, ‘XL‘, ‘L‘, ‘LX‘, ‘LXX‘, ‘LXXX‘, ‘XC‘]
        C = [‘‘, ‘C‘, ‘CC‘, ‘CCC‘, ‘CD‘, ‘D‘, ‘DC‘, ‘DCC‘, ‘DCCC‘, ‘CM‘]
        M = [‘‘, ‘M‘, ‘MM‘, ‘MMM‘]
        thousand = num // 1000
        houndred = (num % 1000) // 100
        ten = (num % 100) // 10
        digit = num % 10
        return M[thousand] + C[houndred] + X[ten] + I[digit]
 
 
s = Solution()
# num = 1
# num = 19
# num = 99
# num = 100
num = 409
#num = 1999
res = s.intToRoman(num)
print(res)






12. Integer to Roman 整数转罗马数字

原文:https://www.cnblogs.com/xiejunzhao/p/8445811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!