您的答案為: XXX 正確答案為: XVIII
程式如下:
RomeAlpha = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
RomeVal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
def RomeToInt (RStr) :
Value = 0
tstr = RStr
while tstr :
for i in range(len(RomeAlpha)):
if tstr.startswith(RomeAlpha[i]) :
Value += RomeVal[i]
tstr = tstr[len(RomeAlpha[i]):]
break
return Value
def IntToRome (Val) :
RomeStr = ''
while Val > 0 :
for i in range(len(RomeVal)) :
if Val >= RomeVal[i] :
RomeStr += RomeAlpha[i]
Val -= RomeVal[i]
break
return RomeStr
while True :
iStr = input()
if iStr == '#' :
break
[a, b] = iStr.split()
a = RomeToInt(a)
b = RomeToInt(b)
Result = a - b
if not Result :
print ('ZERO')
else :
print ( IntToRome(Result))
#0: 100% WA (line:8)
您的答案為: XXX 正確答案為: XVIII
程式如下:RomeAlpha = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
RomeVal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]def RomeToInt (RStr) :
Value = 0
tstr = RStrwhile tstr :
for i in range(len(RomeAlpha)):
if tstr.startswith(RomeAlpha[i]) :
Value += RomeVal[i]
tstr = tstr[len(RomeAlpha[i]):]
break
return Valuedef IntToRome (Val) :
RomeStr = ''
while Val > 0 :
for i in range(len(RomeVal)) :
if Val >= RomeVal[i] :
RomeStr += RomeAlpha[i]
Val -= RomeVal[i]
break
return RomeStrwhile True :
iStr = input()
if iStr == '#' :
break
[a, b] = iStr.split()
a = RomeToInt(a)
b = RomeToInt(b)Result = a - b
if not Result :
print ('ZERO')
else :
print ( IntToRome(Result))
Result = a - b 要修改為 Result = abs ( a - b)