# 定義羅馬數字和阿拉伯數字之間的對應關係
roman_to_int_map = {
'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000
}
# 將羅馬數字轉換成阿拉伯數字
def roman_to_int(roman):
total = 0
prev_value = 0
for char in reversed(roman):
value = roman_to_int_map[char]
if value < prev_value:
total -= value
else:
total += value
prev_value = value
return total
# 將阿拉伯數字轉換成羅馬數字
def int_to_roman(num):
int_to_roman_map = [
(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')
]
result = ""
for value, symbol in int_to_roman_map:
while num >= value:
result += symbol
num -= value
return result
# 處理每行測試資料,輸出兩個數字差的絕對值並轉換成羅馬數字
def process_lines():
results = []
while True:
line = input().strip()
if line == "#":
break
num1_roman, num2_roman = line.split()
num1 = roman_to_int(num1_roman)
num2 = roman_to_int(num2_roman)
diff = abs(num1 - num2)
if diff == 0:
results.append("ZERO")
else:
results.append(int_to_roman(diff))
# 輸出結果
for result in results:
print(result)
# 執行處理函數
process_lines()