結果

問題 No.518 ローマ数字の和
ユーザー momen999momen999
提出日時 2017-05-31 18:25:30
言語 Haskell
(9.6.2)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 6,624 ms
コンパイル使用メモリ 163,440 KB
実行使用メモリ 5,296 KB
最終ジャッジ日時 2023-10-21 19:07:25
合計ジャッジ時間 3,174 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,232 KB
testcase_01 AC 2 ms
5,232 KB
testcase_02 AC 2 ms
5,168 KB
testcase_03 AC 2 ms
5,240 KB
testcase_04 AC 2 ms
5,232 KB
testcase_05 AC 2 ms
5,168 KB
testcase_06 AC 2 ms
5,236 KB
testcase_07 AC 2 ms
5,236 KB
testcase_08 AC 2 ms
5,296 KB
testcase_09 AC 3 ms
5,232 KB
testcase_10 AC 2 ms
5,236 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,236 KB
testcase_13 AC 2 ms
5,236 KB
testcase_14 AC 2 ms
5,228 KB
testcase_15 AC 2 ms
5,232 KB
testcase_16 AC 3 ms
5,236 KB
testcase_17 AC 2 ms
5,232 KB
testcase_18 AC 2 ms
5,232 KB
testcase_19 AC 2 ms
5,228 KB
testcase_20 AC 2 ms
5,236 KB
testcase_21 AC 3 ms
5,164 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

toNum ns = iter 0 "" (reverse ns)
    where
        iter x _ [] = x
        iter x s (n:ns)
            | n == 'V'          = iter (x + 5) "V" ns
            | n == 'L'          = iter (x + 50) "L" ns
            | n == 'D'          = iter (x + 500) "D" ns
            | n == 'M'          = iter (x + 1000) "M" ns
            | n == 'I'          = case s of { "V" -> iter (x - 1) "" ns; "X" -> iter (x - 1) "" ns; _ -> iter (x + 1) "" ns; }
            | n == 'X'          = case s of { "L" -> iter (x - 10) "" ns; "C" -> iter (x - 10) "" ns; _ -> iter (x + 10) "X" ns; }
            | n == 'C'          = case s of { "D" -> iter (x - 100) "" ns; "M" -> iter (x - 100) "" ns; _ -> iter (x + 100) "C" ns; }
            | otherwise         = 0

toRoma n
    | n < 4000  = p1000 (f 3) ++ p100 (f 2) ++ p10 (f 1) ++ p1 (f 0)
    | otherwise = "ERROR"
    where
        f m = (n `div` (10^m)) `mod` 10
        p1000 x | x == 0    = ""
                | otherwise = "M" ++ p1000 (x - 1)
        p100 x  | x == 0    = ""
                | x == 9    = "CM"
                | x == 4    = "CD"
                | x >= 5    = "D" ++ p100 (x - 5)
                | otherwise = "C" ++ p100 (x - 1)
        p10 x   | x == 0    = ""
                | x == 9    = "XC"
                | x == 4    = "XL"
                | x >= 5    = "L" ++ p10 (x - 5)
                | otherwise = "X" ++ p10 (x - 1)
        p1 x    | x == 0    = ""
                | x == 9    = "IX"
                | x == 4    = "IV"
                | x >= 5    = "V" ++ p1 (x - 5)
                | otherwise = "I" ++ p1 (x - 1)

main = do
    getLine
    ns <- map toNum . words <$> getLine
    putStrLn $ toRoma $ sum ns
0