美文网首页
整数转罗马数字

整数转罗马数字

作者: 地铁姑娘 | 来源:发表于2018-09-14 14:36 被阅读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

    相关文章

      网友评论

          本文标题:整数转罗马数字

          本文链接:https://www.haomeiwen.com/subject/pdeogftx.html