結果

問題 No.518 ローマ数字の和
ユーザー vwxyzvwxyz
提出日時 2022-09-22 06:56:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,478 bytes
コンパイル時間 112 ms
コンパイル使用メモリ 11,128 KB
実行使用メモリ 10,680 KB
最終ジャッジ日時 2023-08-23 20:23:46
合計ジャッジ時間 2,006 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
10,420 KB
testcase_01 AC 34 ms
10,664 KB
testcase_02 AC 35 ms
10,464 KB
testcase_03 AC 34 ms
10,520 KB
testcase_04 AC 34 ms
10,680 KB
testcase_05 AC 34 ms
10,416 KB
testcase_06 AC 34 ms
10,608 KB
testcase_07 AC 34 ms
10,520 KB
testcase_08 AC 34 ms
10,524 KB
testcase_09 AC 33 ms
10,600 KB
testcase_10 AC 34 ms
10,616 KB
testcase_11 AC 33 ms
10,536 KB
testcase_12 AC 34 ms
10,632 KB
testcase_13 AC 33 ms
10,668 KB
testcase_14 AC 35 ms
10,668 KB
testcase_15 AC 35 ms
10,664 KB
testcase_16 AC 34 ms
10,528 KB
testcase_17 AC 33 ms
10,564 KB
testcase_18 AC 34 ms
10,588 KB
testcase_19 AC 34 ms
10,468 KB
testcase_20 AC 34 ms
10,544 KB
testcase_21 AC 34 ms
10,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
write=sys.stdout.write

N=int(readline())
num=0
for R in readline().split():
    while R:
        if len(R)>=2 and R[:2] in ("IV","IX","XL","XC","CD","CM"):
            if R[:2]=="IV":
                num+=4
            elif R[:2]=="IX":
                num+=9
            elif R[:2]=="XL":
                num+=40
            elif R[:2]=="XC":
                num+=90
            elif R[:2]=="CD":
                num+=400
            elif R[:2]=="CM":
                num+=900
            R=R[2:]
        else:
            for i in range(len(R)):
                if R[0]!=R[i]:
                    break
            else:
                i=len(R)
            if R[0]=="M":
                num+=1000*i
            elif R[0]=="D":
                num+=500*i
            elif R[0]=="C":
                num+=100*i
            elif R[0]=="L":
                num+=50*i
            elif R[0]=="X":
                num+=10*i
            elif R[0]=="V":
                num+=5*i
            elif R[0]=="I":
                num+=i
            R=R[i:]
ans_lst=[]
if num>=4000:
    ans_lst.append("ERROR")
else:
    x=num//1000
    num%=1000
    if x:
        ans_lst.append("M"*x)
    x=num//100
    num%=100
    if x==9:
        ans_lst.append("CM")
    elif 5<=x<=8:
        ans_lst.append("D"+"C"*(x-5))
    elif x==4:
        ans_lst.append("CD")
    elif x:
        ans_lst.append("C"*x)
    x=num//10
    num%=10
    if x==9:
        ans_lst.append("XC")
    elif 5<=x<=8:
        ans_lst.append("L"+"X"*(x-5))
    elif x==4:
        ans_lst.append("XL")
    elif x:
        ans_lst.append("X"*x)
    x=num
    if x==9:
        ans_lst.append("IX")
    elif 5<=x<=8:
        ans_lst.append("V"+"I"*(x-5))
    elif x==4:
        ans_lst.append("IV")
    elif x:
        ans_lst.append("I"*x)
print(*ans_lst,sep="")
0