整数转罗马数字

2018-09-14  本文已影响0人  地铁姑娘
#encoding=utf-8
class Solution(object):
    def inToRoman(self,num):
        '''
        :param num: int
        :return: str
        '''
        if num>3999 or num<1:
            return ''
        num_tuple = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
        roman_tuple = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
        result_str = ""
        for i in range(len(num_tuple)):
            while num>=num_tuple[i]:
                num -=  num_tuple[i]
                result_str += roman_tuple[i]
        return result_str
if __name__ == "__main__":
    one = [77,66,55,8,1200]
    s = Solution()
    for ele in one:
        print ele,"------>",s.inToRoman(ele)

结果是:

image.png
上一篇下一篇

猜你喜欢

热点阅读