#37340: python


fxxkuyt@gmail.com (GG ez)

學校 : 不指定學校
編號 : 228972
來源 : [163.21.252.253]
最後登入時間 :
2023-09-22 11:27:25
a013. 羅馬數字 -- NPSC 模擬試題 | From: [49.216.90.164] | 發表日期 : 2023-09-02 13:43


def roman_to_int(roman):
    roman_dict = {
        'I': 1, 'V': 5, 'X': 10,
        'L': 50, 'C': 100, 'D': 500, 'M': 1000
    }
    num = 0
    prev_value = 0
    for symbol in roman:
        value = roman_dict[symbol]
        if value > prev_value:
            num += value - 2 * prev_value
        else:
            num += value
        prev_value = value
    return num

# 定义将整数转换为罗马数字的函数
def int_to_roman(n):
    val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    syms = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
    roman_num = ''
    for i in range(len(val)):
        while n >= val[i]:
            roman_num += syms[i]
            n -= val[i]
    return roman_num


while True:
    line = input()
    if line == '#':
        break
    num1, num2 = map(roman_to_int, line.split())
    diff = abs(num1 - num2)
    if diff == 0:
        print("ZERO")
    else:
        roman_diff = int_to_roman(diff)
        print(roman_diff)

 
ZeroJudge Forum