Python程序:将1到3999之间的罗马数字转换为十进制
罗马数字是基于罗马数字系统的一种数字表示法中使用的字符。所有主要符号都在以下部分中介绍。在本问题中,我们得到一个罗马数字字符串,我们的任务是将范围为1到3999的罗马数字转换为十进制。
以下是一些示例和解释,以帮助您更好地理解问题。
输入
str = “MXCIX”
输出
1099
解释
M是1000的罗马数字表示。
XC是90的罗马数字表示。
IX是9的罗马数字表示。
输入
str = "II"
输出
2
解释
II是2的罗马数字表示。
输入
str = “XL”
输出
40
解释
XL是40的罗马数字表示。
在讨论方法之前,让我们先看看主要的罗马数字符号。罗马数字完全基于以下符号:
符号 | 值 |
---|---|
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等)。但是,在某些情况下,它也遵循减法表示法,以防止连续重复四个字符(例如CCCC)。
I出现在X或V之前表示少一。-> 4用罗马数字表示为IV(比五少一),-> 9用罗马数字表示为IX(比十少一)
X出现在C或L之前表示少十。-> 40用罗马数字表示为XL(比五十少十),-> 90用罗马数字表示为XC(比一百少十)
C出现在M和D之前表示少一百 -> 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直接将罗马数字转换为十进制。为此,只需要使用pip安装Roman模块。
pip install roman
成功安装roman模块后,可以使用fromRoman()方法进行罗马数字到十进制的转换。
它接收罗马数值作为参数,并输出十进制数字。
示例
下面是一个Python程序,使用上述方法将罗马数字转换为十进制。
import roman egFirst = roman.fromRoman("MXCIX") egSecond = roman.fromRoman("II") egThird = roman.fromRoman("XL") print("Roman to Decimal conversions are:") print(egFirst) print(egSecond) print(egThird)
输出
Roman to Decimal conversions are: 1099 2 40
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
方法3
这里使用're'模块。这种方法的思想很简单,我们在这里使用Python的字典函数,将罗马数字与其对应的十进制值关联起来。该程序接受罗马数字作为输入,并将其转换为对应的十进制值。
示例
# Python Program to convert Roman Numerals to Decimal import re # Given string of Roman value strRoman = 'MXCIX' # putting Roman numerals and their associated values in a dictionary romanValue = {'M':1000, 'X':10, 'L':50, 'V':5, 'C':100, 'D':500, 'I':1} # Creating the variable decValue and assign zero to it decValue = 0 n = len(strRoman) # Adding each value from the Roman Numeral string through a loop to the decValue variable 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]] # Printing the Roman numeral in decimal form print("The decimal Numeral form of the Roman Numeral", strRoman, "is") print(decValue)
输出
The decimal Numeral form of the Roman Numeral MXCIX is 1099
结论
在本教程中,我们实现了一个Python程序,用于将1到3999之间的罗马数字转换为十进制。我们实现了三种方法。第一种是使用普通函数,第二种是使用Roman模块,第三种是使用re模块(使用字典)。