顯然我的答案:M'MC''CX'VII,''內的次序顛倒了,不確定問題出在哪。
自己測試輸入MM III時明明能得到正確答案。
程式碼如下:
rom_table = dict(zip( ['I','V','X','L','C','D','M'], [1,5,10,50,100,500,1000] )) def Roman_Number(roman): val = 0 for r in roman: val += rom_table[r] return val num_table = [ [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'] ] def Number_Roman(difference): dfr = difference ans = '' for t in num_table: k = divmod(dfr,t[0]) ans += t[1]*k[0] dfr = k[1] return ans while True: num = input().split() if (num ==['#']): break a = Roman_Number(num[0]) b = Roman_Number(num[1]) c = abs(a-b) if (c == 0): print('ZERO') else: print(Number_Roman(c))
顯然我的答案:M'MC''CX'VII,''內的次序顛倒了,不確定問題出在哪。
自己測試輸入MM III時明明能得到正確答案。
程式碼如下:
rom_table = dict(zip( ['I','V','X','L','C','D','M'], [1,5,10,50,100,500,1000] )) def Roman_Number(roman): val = 0 for r in roman: val += rom_table[r] return val num_table = [ [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'] ] def Number_Roman(difference): dfr = difference ans = '' for t in num_table: k = divmod(dfr,t[0]) ans += t[1]*k[0] dfr = k[1] return ans while True: num = input().split() if (num ==['#']): break a = Roman_Number(num[0]) b = Roman_Number(num[1]) c = abs(a-b) if (c == 0): print('ZERO') else: print(Number_Roman(c))
你羅馬數字轉數字沒考慮減法規則