반응형
https://www.acmicpc.net/problem/2608
input1 = input()
input2 = input()
dct1 = {
'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100,
'D': 500, 'M': 1000
}
dct2 = {
'IV': 4, 'IX': 9, 'XL': 40,
'XC': 90, 'CD': 400, 'CM': 900
}
def toArab(rome):
idx = 0
num = 0
while idx < len(rome):
cur = rome[idx]
if cur in 'IXC' and idx != len(rome)-1:
next = rome[idx+1]
if cur+next in dct2:
num += dct2[cur+next]
idx += 2
continue
num += dct1[cur]
idx += 1
return num
def toRome(num):
rome = ""
arab = [x for x in str(num)]
pos = len(arab)-1
lst = ['I', 'X', 'C', 'M'] # 1, 10, 100, 1000
lst2 = ['V', 'L', 'D'] # 5,50,500
lst3 = ['IV', 'XL', 'CD'] # 4 40 400
lst4 = ['IX', 'XC', 'CM'] # 9 90 900
while arab:
c = arab.pop(0)
if c == '4': # special case
rome += lst3[pos]
elif c == '9':
rome += lst4[pos]
elif c in '123': # less than 5
for _ in range(int(c)):
rome += lst[pos]
elif c in '5678': # bigger than 5
rome += lst2[pos]
if c != '5':
for _ in range(int(c)-5):
rome += lst[pos]
pos -= 1
return rome
arab_num = toArab(input1)+toArab(input2)
print(arab_num)
print(toRome(arab_num))
댓글