結果

問題 No.518 ローマ数字の和
ユーザー 👑 yumechiyumechi
提出日時 2017-05-28 22:32:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 1,866 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 11,988 KB
実行使用メモリ 10,124 KB
最終ジャッジ日時 2023-10-21 14:24:01
合計ジャッジ時間 1,894 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,124 KB
testcase_01 AC 30 ms
10,124 KB
testcase_02 AC 28 ms
10,124 KB
testcase_03 AC 29 ms
10,124 KB
testcase_04 AC 29 ms
10,124 KB
testcase_05 AC 29 ms
10,124 KB
testcase_06 AC 29 ms
10,124 KB
testcase_07 AC 34 ms
10,124 KB
testcase_08 AC 29 ms
10,124 KB
testcase_09 AC 29 ms
10,124 KB
testcase_10 AC 30 ms
10,124 KB
testcase_11 AC 29 ms
10,124 KB
testcase_12 AC 29 ms
10,124 KB
testcase_13 AC 29 ms
10,124 KB
testcase_14 AC 29 ms
10,124 KB
testcase_15 AC 29 ms
10,124 KB
testcase_16 AC 29 ms
10,124 KB
testcase_17 AC 29 ms
10,124 KB
testcase_18 AC 30 ms
10,124 KB
testcase_19 AC 28 ms
10,124 KB
testcase_20 AC 28 ms
10,124 KB
testcase_21 AC 29 ms
10,124 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:
    roman = ""
    while arabic > 0:
        if arabic >= 1000:
            roman += "M"
            arabic -= 1000
        elif arabic >= 900:
            roman += "CM"
            arabic -= 900
        elif arabic >= 500:
            roman += "D"
            arabic -= 500
        elif arabic >= 400:
            roman += "CD"
            arabic -= 400
        elif arabic >= 100:
            roman += "C"
            arabic -= 100
        elif arabic >= 90:
            roman += "XC"
            arabic -= 90
        elif arabic >= 50:
            roman += "L"
            arabic -= 50
        elif arabic >= 40:
            roman += "XL"
            arabic -= 40
        elif arabic >= 10:
            roman += "X"
            arabic -= 10
        elif arabic >= 9:
            roman += "IX"
            arabic -= 9
        elif arabic >= 5:
            roman += "V"
            arabic -= 5
        elif arabic >= 4:
            roman += "IV"
            arabic -= 4
        else:
            roman += "I"
            arabic -= 1
    return roman 

def solve():
    n = input()
    ans = 0
    for roman in input().split():
        ans += roman2arabic(roman)
    if ans > 3999:
        print("ERROR")
        return
    # print(ans)
    ans_roman = arabic2roman(ans)
    print(ans_roman)

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