#41276: python


suyueh (suyueh)

學校 : 不指定學校
編號 : 272111
來源 : [101.12.162.212]
最後登入時間 :
2024-11-10 22:20:41
a013. 羅馬數字 -- NPSC 模擬試題 | From: [125.229.229.84] | 發表日期 : 2024-07-16 13:58

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