結果

問題 No.518 ローマ数字の和
ユーザー tottoripapertottoripaper
提出日時 2018-03-13 17:17:01
言語 Haskell
(9.6.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,647 bytes
コンパイル時間 4,398 ms
コンパイル使用メモリ 139,148 KB
最終ジャッジ日時 2023-08-14 12:31:40
合計ジャッジ時間 5,824 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:1:1: error:
    Could not load module ‘Text.Parsec’
    It is a member of the hidden package ‘parsec-3.1.16.1’.
    You can run ‘:set -package parsec’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
1 | import           Text.Parsec
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ソースコード

diff #

import           Text.Parsec

n4 = do
  mn <- length <$> many (char 'M')
  n <- n3
  return $ mn * 1000 + n

n3 = try n31 <|> try n32 <|> n33
  where
    n31 = string "CD" >> (+ 400) <$> n2
    n32 = string "CM" >> (+ 900) <$> n2
    n33 = do
      d <- length <$> many (char 'D')
      cn <- length <$> many (char 'C')
      n <- n2
      return $ d * 500 + cn * 100 + n

n2 = try n21 <|> try n22 <|> n23
  where
    n21 = string "XL" >> (+ 40) <$> n1
    n22 = string "XC" >> (+ 90) <$> n1
    n23 = do
      l <- length <$> many (char 'L')
      xn <- length <$> many (char 'X')
      n <- n1
      return $ l * 50 + xn * 10 + n

n1 = try n11 <|> try n12 <|> n13
  where
    n11 = string "IV" >> return 4
    n12 = string "IX" >> return 9
    n13 = do
      v <- length <$> many (char 'V')
      i_n <- length <$> many (char 'I')
      return $ v * 5 + i_n

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 parse' :: String -> Int
      parse' y = case parse n4 "" y of
                   Left _  -> undefined
                   Right n -> n
  let s = sum $ map parse' rs
  putStrLn $ m s
0