将罗马数字转换为 1 至 3999 之间的十进制数的 Python 程序
罗马数字是基于前罗马系统的数字符号排列中使用的字符。以下部分涵盖了所有主要符号。在这个问题中,我们给出了一串罗马数字,我们的任务是将罗马数字转换为 1 到 3999 范围内的十进制数。
下面是一些示例和解释,以帮助您更好地理解问题。
输入
str = "MXCIX"
输出
1099
解释
M 是 1000 的罗马表示,
XC 是 90 的罗马表示,
IX 是 9 的罗马表示。
输入
str = "II"
输出
2
解释
II 是 2 的罗马表示。
输入
str = "XL"
输出
40
解释
XL 是 40 的罗马表示。
在介绍方法之前,让我们先看看主要的罗马符号。 Roman 完全围绕以下符号构建:-
SYMBOL | VALUE |
---|---|
M | 1000 |
CM | 900 |
D | 500 |
CD | 400 |
C | 100 |
XC | 90 |
L | 50 |
XL | 40 |
X | 10 |
IX | 9 |
V | 5 |
IV | 4 |
I | 1 |
方法
根据观察,罗马数字符号遵循降序来表示数字(例如,M 在前,C 在后,等等)。但是,在某些情况下,它也遵循减法符号,以防止连续重复 4 个字符(例如 CCCC):
I 位于 X 或 V 之前,表示少一个。 -> 4 在罗马数字中表示为 IV(比五少一个), -> 9 在罗马数字中表示为 IX(比 10 少 1)
X 位于 C 或 L 之前,表示少十个。 -> 40 在罗马数字中表示为 XL(比五十少十个), -> 90 用罗马数字表示为 XC(比 100 少 10)
C 位于 M 和 D 之前,表示比 100 少 -> 400 用罗马数字表示为 CD(比五百少百) -> 900 用罗马数字表示为 CM(比千少百)
为了进一步理解上述方法,让我们看看下面的代码。
示例
Python 程序将 1 到 3999 之间的罗马数字转换为十进制数。
# Function is created to return a Roman symbol's value. def value(ch): val = -1 if(ch=='I'): val = 1 if(ch=='V'): val = 5 if(ch=='X'): val = 10 if(ch=='L'): val = 50 if(ch=='C'): val = 100 if(ch=='D'): val = 500 if(ch=='M'): val = 1000 return val def convertRomanToDecimal(str): decVal = 0 i = 0 n = len(str) # store the size of the string while (i < n): # store the numeric value of roman value str[i] current = value(str[i]) # Check if i+1 charchter exists or not if (i + 1 < n): # store the numeric value of roman value str[i+1] next = value(str[ i + 1 ]) # Check which value is greater current or next if (current >= next): # If current >= next add current # value to the variable decVal decVal = decVal + current # Increment the index of the string to point to the next char i = i + 1 else: # If current<next add difference of next to current to the variable decVal decVal = decVal + next - current # Increment the index of the string to point to the next char i = i + 2 else: decVal = decVal + current # Increment the index of the string to point to the next char i = i + 1 return decVal print("The decimal Numeral form of the Roman Numeral is"), print(convertRomanToDecimal("MXCIX"))
输出
The decimal Numeral form of the Roman Numeral is 1099
时间和空间复杂度
上述代码的时间复杂度为 O(N),其中 N 是字符串的大小。由于代码中未使用额外空间,因此上述代码的空间复杂度为 O(1)。
方法 2
在此方法中,我们使用内置模块 Roman 直接将 Roman 转换为十进制。为此,只需使用 pip 安装 Roman 模块即可。
pip install roman
成功安装模块 roman 后,使用方法 fromRoman() 完成从罗马数字到十进制的转换。
它以罗马值的形式接收参数并输出十进制数。
示例
下面是一个 Python 程序,用于实现上述方法将罗马数字转换为十进制。
import roman egFirst = roman.fromRoman("MXCIX") egSecond = roman.fromRoman("II") egThird = roman.fromRoman("XL") print("罗马数字到十进制转换是:") print(egFirst) print(egSecond) print(egThird)
输出
罗马数字到十进制转换是: 1099 2 40
方法 3
这里使用"re"模块。这种方法的思路很简单,这里我们使用 Python 的字典函数将罗马数字与其对应的十进制值联系起来。该程序接受罗马数字作为输入,并将其转换为相应的十进制值。
示例
# Python 程序将罗马数字转换为十进制 import re # 给定罗马值的字符串 strRoman = 'MXCIX' # 将罗马数字及其相关值放入字典中 romanValue = {'M':1000, 'X':10, 'L':50, 'V':5, 'C':100, 'D':500, 'I':1} # 创建变量 decValue 并为其分配零 decValue = 0 n = len(strRoman) # 通过循环将罗马数字字符串中的每个值添加到 decValue 变量中 for i in range(n): if i > 0 and romanValue[strRoman[i]] >romanValue[strRoman[i-1]]: decValue += romanValue[strRoman[i]] - 2 * romanValue[strRoman[i-1]] else: decValue += romanValue[strRoman[i]] # 以十进制形式打印罗马数字 print("罗马数字的十进制数字形式", strRoman, "is") print(decValue)
输出
罗马数字的十进制数字形式 MXCIX 1099
结论
在本教程中,我们实现了一个 Python 程序,用于将 1 到 3999 之间的罗马数字转换为十进制。我们实现了三种方法。第一种使用普通函数,第二种使用 Roman 模块,第三种使用 re 模块(使用字典)。