我想问一下我是否应该继续这种想法/罗马数字转换器的方式,还是应该考虑使用diff代码。
这是我目前的想法:
d_roman = { "I":1, "V": 5, "X":10}
rntconvert = raw_input("Enter Roman Num: " )
x = len(rntconvert)
if rntcovert is in dictionary
#print value of rntcovert
elif x==2
#add the value of two roman numbers except if first roman number is lower than the next then it will be subtracted
#(ex. IX, first char has lower value than next char, so it's like, 10 - 1)
这可行吗?先感谢您。
编辑:即时通讯使用2.7说明是:我按返回的字符串数量分隔了求解
您的想法是正确的,但坦率地说,它的表达不佳,并且只能使用最多两个字母的字符串。将来可能会在尝试实现算法之前帮助制定更好的公式,但是暂时,我可以举一个例子说明如何实现该算法:
def from_roman_to_arabic(n):
numerals={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
adjust_values={'IV':2,'IX':2,'XL':20,'XC':20,'CD':200,'CM':200}
s=0
for i in n:
s+=numerals[i]
for j in adjust_values:
if j in n:
s-=adjust_values[j]
return s
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句