結果

問題 No.518 ローマ数字の和
ユーザー 👑 yumechiyumechi
提出日時 2017-06-18 17:03:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,078 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-09 20:05:34
合計ジャッジ時間 1,527 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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