結果

問題 No.518 ローマ数字の和
ユーザー 👑 yumechiyumechi
提出日時 2017-05-28 22:47:29
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 12,068 KB
実行使用メモリ 10,044 KB
最終ジャッジ日時 2023-10-21 14:27:33
合計ジャッジ時間 1,382 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

def roman2arabic(roman: str) -> int:
    special_case = { "IV" : 4, "IX" : 9, "XL" : 40, "XC" : 90, "CD" : 400, "CM" : 900 }
    normal_case = { "I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000 }
    arabic = 0
    for s in special_case.keys():
        if s in roman:
            arabic += special_case[s]
            roman = roman.replace(s, "")
    for n in normal_case.keys():
        if n in roman:
            arabic += roman.count(n) * normal_case[n]
    return arabic

def arabic2roman(arabic: int) -> str:
    if arabic > 3999:
        return "ERROR"
    roman = ""
    roman_numerics = { 1000 : "M", 900 : "CM", 500 : "D", 400 : "CD", 100 : "C", 90 : "XC", 50 : "L", 40 : "XL", 10 : "X", 9 : "IX", 5 : "V", 4 : "IV", 1 : "I" }
    while arabic > 0:
        for n in roman_numerics.keys():
            if arabic >= n:
                roman += roman_numerics[n]
                arabic -= n
                break
    return roman 

def solve():
    input()
    print(arabic2roman(sum([roman2arabic(i) for i in input().split()])))

if __name__=="__main__":
    solve()
0