結果

問題 No.518 ローマ数字の和
ユーザー tottoripapertottoripaper
提出日時 2018-03-13 17:35:01
言語 Haskell
(9.8.2)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,025 bytes
コンパイル時間 4,778 ms
コンパイル使用メモリ 172,160 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-02 01:11:21
合計ジャッジ時間 5,700 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

n_ ""           = 0
n_ ('M':xs)     = 1000 + n_ xs
n_ ('C':'D':xs) = 400 + n_ xs
n_ ('C':'M':xs) = 900 + n_ xs
n_ ('D':xs)     = 500 + n_ xs
n_ ('C':xs)     = 100 + n_ xs
n_ ('X':'L':xs) = 40 + n_ xs
n_ ('X':'C':xs) = 90 + n_ xs
n_ ('L':xs)     = 50 + n_ xs
n_ ('X':xs)     = 10 + n_ xs
n_ "IV"         = 4
n_ "IX"         = 9
n_ ('V':xs)     = 5 + n_ xs
n_ xs           = length xs

m x
  | x >= 4000 = "ERROR"
  | otherwise = s4 ++ s3 ++ s2 ++ s1
  where
    [x1, x2, x3, x4] = take 4 $ map (`mod` 10) (iterate (`div` 10) x) ++ replicate 4 0
    f u1 u2 u3 x = if x == 9
                   then [u1, u3]
                   else if x >= 5
                        then u2 : replicate (x - 5) u1
                        else if x == 4
                             then [u1, u2]
                             else replicate x u1
    s4 = replicate x4 'M'
    s3 = f 'C' 'D' 'M' x3
    s2 = f 'X' 'L' 'C' x2
    s1 = f 'I' 'V' 'X' x1

main = do
  _ <- getLine
  rs <- words <$> getLine
  let s = sum $ map n_ rs
  putStrLn $ m s
0