当然可以!以下是一个简单的 罗马数字转换器 的实现方式,你可以将罗马数字转换为对应的整数值。
✅ 罗马数字转换器(Python 实现)
def roman_to_int(roman):
roman_dict = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
total = 0
prev_value = 0
for char in roman:
current_value = roman_dict[char]
if current_value > prev_value:
total -= prev_value
else:
total += current_value
prev_value = current_value
return total
# 示例用法
roman = input("请输入罗马数字(例如:MCMXCIV):")
result = roman_to_int(roman)
print(f"罗马数字 {roman} 对应的整数值是: {result}")