12. 整数转罗马数字

class Solution:
    def intToRoman(self, num: int) -> str:
        s = ""
        if num >= 1000:
            s += num // 1000 * "M"
            num = int(str(num)[-3:])
        if 1000 > num >= 500:
            if 1000 > num >= 900:
                s += "CM"
                num = int(str(num)[-2:])
            else:
                s += "D"
                num -= 500
        if 500 > num >= 100:
            if 500 > num >= 400:
                s += "CD"
                num = int(str(num)[-2:])
            else:
                s += num // 100 * "C"
                num = int(str(num)[-2:])
        if 100 > num >= 50:
            if 100 > num >= 90:
                s += "XC"
                num = int(str(num)[-1])
            else:
                s += "L"
                num -= 50
        if 50 > num >= 10:
            if 50 > num >= 40:
                s += "XL"
                num = int(str(num)[-1])
            else:
                s += num // 10 * "X"
                num = int(str(num)[-1])
        if 10 > num >= 5:
            if num == 9:
                s += "IX"
                return s
            else:
                s += "V"
                num -= 5
        if 5 > num >= 1:
            if num == 4:
                s += "IV"
                return s
            else:
                s += num * "I"
                return s
        if num == 0:
            return s

你可能感兴趣的:(12. 整数转罗马数字)