📜  Python程序将整数转换为罗马

📅  最后修改于: 2022-05-13 01:55:33.802000             🧑  作者: Mango

Python程序将整数转换为罗马

给定一个整数,任务是编写一个Python程序将整数转换为罗马字符。

例子:

Input: 5
Output: V

Input: 9
Output: IX

Input: 40
Output: XL

Input:  1904
Output: MCMIV

下表显示了罗马符号列表,还包括它们相应的整数值:

SymbolsValues
I1
IV  4
V5
IX9
X10
XL40
L50
XC90
C100
CD400
D500
CM900
M1000

想法是将给定数字的单位、十位、百位和千位分别转换。如果数字为 0,则没有相应的罗马数字符号。数字 4 和 9 的转换与其他数字略有不同,因为这些数字遵循减法表示法。

将整数值转换为罗马数字的算法



按照 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 的顺序将给定数字与基值进行比较。 小于或等于给定数字的基值将是初始基值(最大基值),将数除以其最大基值,对应的基符号将重复商数次,余数将成为以后除法和重复的数。该过程将重复,直到数字变为零。

方法一:

  • 最初 number = 3549,因为 3549 >= 1000 ;最初的最大基值将是 1000。并除以 3549/1000。商 = 3,余数 = 549。相应的符号 M 将重复三次。
  • 现在,数字变为 549 并且 1000 > 549 >= 500,最大基值将是 500 然后除以 549/500。商 = 1,余数 = 49。相应的符号 D 将重复一次。
  • 现在,number = 49 和 50 > 49 >= 40,最大基值是 40。然后除以 49/40。商 = 1,余数 = 9。对应的符号 XL 将重复一次。
  • 现在,number = 9 且 10> 9 >= 9,最大基值为 9。然后除以 9/9。商 = 1,余数 = 0。相应的符号 IX 将重复一次。
  • 最后,数字变为 0,算法到此为止。得到的输出为 MMMDXLIX。

下面的例子展示了上述算法的实现:

Python3
# Python3 program to convert
# integer value to roman values
  
# Function to convert integer to Roman values
def printRoman(number):
    num = [1, 4, 5, 9, 10, 40, 50, 90,
        100, 400, 500, 900, 1000]
    sym = ["I", "IV", "V", "IX", "X", "XL",
        "L", "XC", "C", "CD", "D", "CM", "M"]
    i = 12
      
    while number:
        div = number // num[i]
        number %= num[i]
  
        while div:
            print(sym[i], end = "")
            div -= 1
        i -= 1
  
# Driver code
if __name__ == "__main__":
    number = 3549
    print("Roman value is:", end = " ")
    printRoman(number)


Python3
# Python3 program for above approach
  
# Function to calculate Roman values
def intToRoman(num):
  
    # Storing roman values of digits from 0-9
    # when placed at different places
    m = ["", "M", "MM", "MMM"]
    c = ["", "C", "CC", "CCC", "CD", "D",
         "DC", "DCC", "DCCC", "CM "]
    x = ["", "X", "XX", "XXX", "XL", "L",
         "LX", "LXX", "LXXX", "XC"]
    i = ["", "I", "II", "III", "IV", "V",
         "VI", "VII", "VIII", "IX"]
  
    # Converting to roman
    thousands = m[num // 1000]
    hundereds = c[(num % 1000) // 100]
    tens = x[(num % 100) // 10]
    ones = i[num % 10]
  
    ans = (thousands + hundereds +
           tens + ones)
  
    return ans
  
# Driver code
if __name__ == "__main__":
    number = 3549
    print(intToRoman(number))


Python3
# Python 3 program to convert integer
# number to Roman values
import math
  
def integerToRoman(A):
    romansDict = \
        {
            1: "I",
            5: "V",
            10: "X",
            50: "L",
            100: "C",
            500: "D",
            1000: "M",
            5000: "G",
            10000: "H"
        }
  
    div = 1
    while A >= div:
        div *= 10
  
    div /= 10
  
    res = ""
  
    while A:
  
        # main significant digit extracted
        # into lastNum
        lastNum = int(A / div)
  
        if lastNum <= 3:
            res += (romansDict[div] * lastNum)
        elif lastNum == 4:
            res += (romansDict[div] +
                        romansDict[div * 5])
        elif 5 <= lastNum <= 8:
            res += (romansDict[div * 5] +
            (romansDict[div] * (lastNum - 5)))
        elif lastNum == 9:
            res += (romansDict[div] +
                        romansDict[div * 10])
  
        A = math.floor(A % div)
        div /= 10
          
    return res
  
# Driver code
print("Roman value for the integer is:"
                + str(integerToRoman(3549)))


输出:

Roman value is: MMMDXLIX

方法二:

在这种方法中,我们首先要观察问题。问题陈述中给出的数字最多可以是 4 位数字。解决这个问题的思路是:

  • 将给定的数字分成不同位置的数字,如一、二、百或千。
  • 从千位开始打印相应的罗马值。例如,如果千位的数字是 3,则打印相当于 3000 的罗马字。
  • 重复第二步,直到我们到达一个位置。

假设输入数字是 3549。因此,从千位开始,我们将开始打印罗马等价物。在这种情况下,我们将按以下顺序打印:

  • 罗马相当于 3000
  • 罗马相当于 500
  • 罗马相当于 40
  • 罗马相当于 9

因此,输出将是: MMMDXLIX

下面的例子展示了上述方法的实现:

蟒蛇3

# Python3 program for above approach
  
# Function to calculate Roman values
def intToRoman(num):
  
    # Storing roman values of digits from 0-9
    # when placed at different places
    m = ["", "M", "MM", "MMM"]
    c = ["", "C", "CC", "CCC", "CD", "D",
         "DC", "DCC", "DCCC", "CM "]
    x = ["", "X", "XX", "XXX", "XL", "L",
         "LX", "LXX", "LXXX", "XC"]
    i = ["", "I", "II", "III", "IV", "V",
         "VI", "VII", "VIII", "IX"]
  
    # Converting to roman
    thousands = m[num // 1000]
    hundereds = c[(num % 1000) // 100]
    tens = x[(num % 100) // 10]
    ones = i[num % 10]
  
    ans = (thousands + hundereds +
           tens + ones)
  
    return ans
  
# Driver code
if __name__ == "__main__":
    number = 3549
    print(intToRoman(number))

输出:

MMMDXLIX

方法三:

在这种方法中,我们考虑数字中的主要有效数字。例如:在 1234 中,主要有效数字是 1。类似地,在 345 中它是 3。为了提取主要有效数字,我们需要维护一个除数(我们称之为 div),如 1000 代表 1234(自 1234 / 1000 = 1) 和 100 表示 345 (345 / 100 = 3)。另外,让我们维护一个名为 roman numeric = {1 : 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: '的字典M'}

下面的例子展示了上述算法的实现:

蟒蛇3

# Python 3 program to convert integer
# number to Roman values
import math
  
def integerToRoman(A):
    romansDict = \
        {
            1: "I",
            5: "V",
            10: "X",
            50: "L",
            100: "C",
            500: "D",
            1000: "M",
            5000: "G",
            10000: "H"
        }
  
    div = 1
    while A >= div:
        div *= 10
  
    div /= 10
  
    res = ""
  
    while A:
  
        # main significant digit extracted
        # into lastNum
        lastNum = int(A / div)
  
        if lastNum <= 3:
            res += (romansDict[div] * lastNum)
        elif lastNum == 4:
            res += (romansDict[div] +
                        romansDict[div * 5])
        elif 5 <= lastNum <= 8:
            res += (romansDict[div * 5] +
            (romansDict[div] * (lastNum - 5)))
        elif lastNum == 9:
            res += (romansDict[div] +
                        romansDict[div * 10])
  
        A = math.floor(A % div)
        div /= 10
          
    return res
  
# Driver code
print("Roman value for the integer is:"
                + str(integerToRoman(3549)))

输出:

Roman value for the integer is: MMMDXLIX