結果

問題 No.518 ローマ数字の和
ユーザー yumechiyumechi
提出日時 2017-05-28 22:46:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,096 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 12,088 KB
実行使用メモリ 10,076 KB
最終ジャッジ日時 2023-10-21 14:27:31
合計ジャッジ時間 1,812 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 28 ms
9,968 KB
testcase_02 AC 28 ms
9,968 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 28 ms
9,968 KB
testcase_06 AC 27 ms
9,968 KB
testcase_07 AC 28 ms
9,968 KB
testcase_08 AC 28 ms
9,972 KB
testcase_09 WA -
testcase_10 AC 28 ms
9,968 KB
testcase_11 AC 28 ms
9,968 KB
testcase_12 AC 28 ms
9,968 KB
testcase_13 AC 28 ms
9,968 KB
testcase_14 AC 28 ms
9,968 KB
testcase_15 AC 28 ms
9,968 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 28 ms
9,968 KB
testcase_19 AC 30 ms
9,968 KB
testcase_20 AC 30 ms
9,968 KB
testcase_21 AC 28 ms
9,968 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 : "X" }
    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