Basic program to convert integer to Roman numerals?

后端 未结 24 1304
孤独总比滥情好
孤独总比滥情好 2020-11-30 11:52

I\'m trying to write a code that converts a user-inputted integer into its Roman numeral equivalent. What I have so far is:

The point of the generate_

24条回答
  •  醉梦人生
    2020-11-30 12:20

    start subtracting 1000,900... to 1 from A and stops when it finds positive.add corresponding roman to ans and make A to A-i where i is (1,4,5,9,10.....) repeat while A does not become 0.

    def intToRoman(self, A):
        l=[[1,'I'],[4,'IV'],[5,'V'],[9,'IX'],[10,'X'],[40,'XL'],[50,'L'],
           [90,'XC'],[100,'C'],[400,'CD'],[500,'D'],[900,'CM'],[1000,'M']]
        ans=""
        while(A>0):
            for i,j in l[::-1]:
                if A-i>=0:
                    ans+=j
                    A=A-i
                    break
        return ans
    

提交回复
热议问题