結果

問題 No.518 ローマ数字の和
ユーザー 👑 yumechiyumechi
提出日時 2017-06-18 17:03:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,078 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 54,016 KB
最終ジャッジ日時 2024-04-09 20:05:32
合計ジャッジ時間 1,685 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,456 KB
testcase_01 AC 33 ms
52,928 KB
testcase_02 AC 33 ms
53,076 KB
testcase_03 AC 34 ms
52,992 KB
testcase_04 AC 33 ms
52,752 KB
testcase_05 AC 34 ms
52,188 KB
testcase_06 AC 34 ms
53,780 KB
testcase_07 AC 34 ms
53,176 KB
testcase_08 AC 34 ms
53,872 KB
testcase_09 AC 33 ms
52,776 KB
testcase_10 AC 33 ms
52,184 KB
testcase_11 AC 35 ms
52,960 KB
testcase_12 AC 33 ms
53,312 KB
testcase_13 AC 33 ms
53,136 KB
testcase_14 AC 37 ms
53,096 KB
testcase_15 AC 35 ms
54,016 KB
testcase_16 AC 33 ms
53,560 KB
testcase_17 AC 35 ms
52,004 KB
testcase_18 AC 34 ms
52,284 KB
testcase_19 AC 33 ms
53,940 KB
testcase_20 AC 32 ms
53,164 KB
testcase_21 AC 35 ms
52,464 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:
        if s[0] in roman:
            arabic += s[1]
            roman = roman.replace(s[0], "")
    for n in normal_case:
        if n[0] in roman:
            arabic += roman.count(n[0]) * n[1]
    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:
            if arabic >= n[0]:
                roman += n[1]
                arabic -= n[0]
                break
    return roman 

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

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