結果

問題 No.518 ローマ数字の和
ユーザー SchneeSchnee
提出日時 2019-09-28 20:20:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 646 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-14 07:00:29
合計ジャッジ時間 1,853 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,752 KB
testcase_01 AC 30 ms
10,752 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 30 ms
10,624 KB
testcase_04 AC 31 ms
10,624 KB
testcase_05 AC 31 ms
10,752 KB
testcase_06 AC 30 ms
10,624 KB
testcase_07 AC 30 ms
10,624 KB
testcase_08 AC 30 ms
10,624 KB
testcase_09 AC 30 ms
10,752 KB
testcase_10 AC 32 ms
10,752 KB
testcase_11 AC 31 ms
10,752 KB
testcase_12 AC 30 ms
10,624 KB
testcase_13 AC 31 ms
10,752 KB
testcase_14 AC 31 ms
10,624 KB
testcase_15 AC 30 ms
10,752 KB
testcase_16 AC 30 ms
10,752 KB
testcase_17 AC 30 ms
10,752 KB
testcase_18 AC 30 ms
10,624 KB
testcase_19 AC 29 ms
10,624 KB
testcase_20 AC 30 ms
10,624 KB
testcase_21 AC 30 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

rom_dic = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
n = input()
rom_nums = input().split()
s = 0

for rom_num in rom_nums:
    ara_num = [rom_dic[x] for x in rom_num]
    s += ara_num[-1]
    for a, b in zip(ara_num[:-1], ara_num[1:]):
        s += -a if a < b else a

if s > 3999:
    print("ERROR")
    sys.exit(0)

d2r = {5:"V", 10:"X"}
rom_sum = []
trans = ["IVX", "XLC", "CDM", "M  "]
while s:
    s, d = divmod(s, 10)
    r = "I" * d if d < 4 else "V" + "I" * (d - 5) if 4 < d < 9 else "I" + d2r[d + 1]
    rom_sum.append(r.translate(str.maketrans("IVX", trans.pop(0))))
print("".join(reversed(rom_sum)))
0