转换罗马/阿拉伯数字大于4000 [英] Converting roman/arabic numerals higher than 4000
问题描述
我要编写一个程序,将罗马数字转换为阿拉伯数字,反之亦然.我已经有一个可以转换4000以下数字的有效代码.对于大于或等于4000的罗马数字,写它们的新方法是括号表示乘以1000.例如4000是(IV),6010是(VI)X等.感谢您的回答.丹尼尔
I am going to write a program that converts roman numerals to arabic and vice-versa. I already have a working code for converting numbers under 4000. For roman numerals higher than or equal to 4000 the new way to write them is that a parenthesis means multiplied by 1000. So for example 4000 is (IV), 6010 is (VI)X etc. Thankful for answer. Daniel
好的,以便解决此问题.罗马数字没有大于3999的数字,因此写更大数字的方法是在罗马数字周围加上一个小节(或括号),这意味着数字应乘以1000
Ok so to clear up the problem. There is no roman numeral bigger than 3999, so a method to write bigger numbers is to put a bar (or a parenthesis) around a roman numeral meaning that numeral should be multiplied by 1000
当前功能:
def intToRoman(integer):
rlist = romanList = [(1000, "M"),(900, "CM"),(500, "D"),(400, "CD"),(100, "C"),(90, "XC"),(50, "L"),(40, "XL"),(10, "X"),(9, "IX"),(5, "V"),(4, "IV"),(1, "I")]
romanResult = ""
for wholeNumber in rlist:
while integer >= wholeNumber[0]:
integer -= wholeNumber[0]
romanResult += wholeNumber[1]
return romanResult
def romanToInt(numeral):
rlist = romanList = [(1000, "M"),(900, "CM"),(500, "D"),(400, "CD"),(100, "C"),(90, "XC"),(50, "L"),(40, "XL"),(10, "X"),(9, "IX"),(5, "V"),(4, "IV"),(1, "I")]
index = 0
intResult = 0
for integer, romanNumeral in rlist:
while numeral[index : index + len(romanNumeral)] == romanNumeral:
intResult += integer
index += len(romanNumeral)
推荐答案
您只需要两个函数.一种用于高于4000的数字,另一种用于低于4000的数字.
You simply need two functions. One for numbers higher than 4000 and one for numbers below.
def smallIntToRoman(integer):
rlist = romanList = [(1000, "M"),(900, "CM"),(500, "D"),(400, "CD"),(100, "C"),(90, "XC"),(50, "L"),(40, "XL"),(10, "X"),(9, "IX"),(5, "V"),(4, "IV"),(1, "I")]
romanResult = ""
for wholeNumber in rlist:
while integer >= wholeNumber[0]:
integer -= wholeNumber[0]
romanResult += wholeNumber[1]
return romanResult
def bigIntToRoman(integer):
thousands, rest = divmod(integer, 1000)
return "({}){}".format(smallIntToRoman(thousands), smallIntToRoman(rest))
def intToRoman(integer):
if integer >= 4000:
return bigIntToRoman(integer)
else:
return smallIntToRoman(integer)
def smallRomanToInt(numeral):
rlist = romanList = [(1000, "M"),(900, "CM"),(500, "D"),(400, "CD"),(100, "C"),(90, "XC"),(50, "L"),(40, "XL"),(10, "X"),(9, "IX"),(5, "V"),(4, "IV"),(1, "I")]
if checkIfRomanNumeral(numeral) is False:
pass
elif checkIfRomanNumeral(numeral) is True:
index = 0
intResult = 0
for integer, romanNumeral in rlist:
while numeral[index : index + len(romanNumeral)] == romanNumeral:
intResult += integer
index += len(romanNumeral)
def romainToInt(numeral):
if len(numeral) == 0:
return None
int_parts = numeral[1:].split(')') # Better done with regex
if len(int_parts) == 1:
return smallRomainToInt(numeral)
elif len(int_parts) == 2:
big = smallRomainToInt(int_parts[1])
small = smallRomainToInt(int_parts[0])
if big is None or small is None:
return None
else:
return big * 1000 + small
else:
return None
这篇关于转换罗马/阿拉伯数字大于4000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!