結果

問題 No.518 ローマ数字の和
ユーザー AEn
提出日時 2022-08-26 01:55:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2024-10-13 06:03:43
合計ジャッジ時間 1,920 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
R = list(map(str, input().split()))
ss, nn = 'IVXLCDM', [1,5,10,50,100,500,1000]
n = {ss[i]:nn[i] for i in range(7)}
d = {nn[i]:ss[i] for i in range(7)}
s = ['V', 'L', 'D', 'M']
s1 = ['V', 'X']
s2 = ['L', 'C']
s3 = ['D', 'M']
def f1(S):
    l, res = len(S), 0
    for i in range(l):
        if S[i] in s:
            res += n[S[i]]
        elif S[i] == 'I':
            if i+1<l and S[i+1] in s1:
                res -= 1
            else:
                res += 1
        elif S[i] == 'X':
            if i+1<l and S[i+1] in s2:
                res -= 10
            else:
                res += 10
        else:
            if i+1<l and S[i+1] in s3:
                res -= 100
            else:
                res += 100
    return res

ans = 0
for i in range(N):
    ans += f1(R[i])
if ans>3999:
    print('ERROR')
else:
    res = ''
    for i in range(7):
        p = nn[-1-i]
        q = ss[-1-i]
        for j in range(ans//p):
            res += q
            ans -= p
    res = res.replace('VIIII', 'IX')
    res = res.replace('LXXXX', 'XC')
    res = res.replace('DCCCC', 'CM')
    res = res.replace('IIII', 'IV')
    res = res.replace('XXXX', 'XL')
    res = res.replace('CCCC', 'CD')
    print(res)
        

0