結果

問題 No.518 ローマ数字の和
ユーザー SchneeSchnee
提出日時 2019-09-28 18:11:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 882 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-10 12:44:16
合計ジャッジ時間 1,643 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 29 ms
10,752 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 29 ms
10,752 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()

def r2n(s):
    rs = [rom_dic[x] for x in s]
    s = rs[-1]
    for a, b in zip(rs[:-1], rs[1:]):
        s += -a if a < b else a
        print("SAB", s, a, b)
    return s

def n2r(n):
    d2r = {0: "", 1: "I", 2: "II", 3: "III", 4: "IV", 9:"IX"}
    rs = []
    a = ["IVX", "XLC", "CDM", "M  "]
    while n:
        n, d = divmod(n, 10)
        if d < 5:
            r = d2r[d]
        elif 5 <= d < 9:
            r = "V" + d2r[d - 5]
        else:
            r = d2r[9]
        rs.append(r.translate(str.maketrans("IVX", a.pop(0))))
    return "".join(reversed(rs))

def main(argv):
    s = sum([r2n(x) for x in rom_nums])
    if s > 3999:
        print("ERROR")
        sys.exit(0)
    print(n2r(s))


if __name__ == "__main__":
    main(sys.argv)
0