結果

問題 No.518 ローマ数字の和
ユーザー momen999momen999
提出日時 2017-05-31 17:40:46
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 1,677 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 163,560 KB
実行使用メモリ 5,368 KB
最終ジャッジ日時 2023-10-21 19:05:30
合計ジャッジ時間 2,935 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 3 ms
5,276 KB
testcase_02 AC 2 ms
5,212 KB
testcase_03 AC 2 ms
5,284 KB
testcase_04 AC 2 ms
5,276 KB
testcase_05 AC 2 ms
5,212 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,280 KB
testcase_08 WA -
testcase_09 AC 2 ms
5,276 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
5,280 KB
testcase_13 WA -
testcase_14 AC 2 ms
5,272 KB
testcase_15 AC 2 ms
5,276 KB
testcase_16 AC 2 ms
5,280 KB
testcase_17 AC 2 ms
5,276 KB
testcase_18 AC 2 ms
5,276 KB
testcase_19 AC 2 ms
5,272 KB
testcase_20 AC 2 ms
5,280 KB
testcase_21 AC 3 ms
5,208 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) "" 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