結果

問題 No.518 ローマ数字の和
ユーザー はむ吉🐹はむ吉🐹
提出日時 2017-05-28 21:54:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 851 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 11,972 KB
実行使用メモリ 10,036 KB
最終ジャッジ日時 2023-10-21 14:09:51
合計ジャッジ時間 1,576 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#!/usr/bin/env python3


MAX_NUM = 3999
ROMAN_VALS = (('I', 1), ('IV', 4), ('V', 5), ('IX', 9), ('X', 10), ('XL', 40),
              ('L', 50), ('XC', 90), ('C', 100), ('CD', 400), ('D', 500),
              ('CM', 900), ('M', 1000))[::-1]


def roman2arabic(roman):
    res = 0
    for s, v in ROMAN_VALS:
        while roman.startswith(s):
            res += v
            roman = roman[len(s):]
    return res


def arabic2roman(x):
    res = ""
    for s, v in ROMAN_VALS:
        d, x = divmod(x, v)
        res += d * s
    return res


def sum_romans(romans):
    s = sum(roman2arabic(roman) for roman in romans)
    if s > MAX_NUM:
        return None
    else:
        return arabic2roman(s)


def main():
    _ = input()
    res = sum_romans(input().split())
    print("ERROR" if res is None else res)


if __name__ == '__main__':
    main()
0