結果

問題 No.518 ローマ数字の和
ユーザー aimyaimy
提出日時 2017-05-29 22:37:21
言語 Haskell
(9.8.2)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,089 bytes
コンパイル時間 7,710 ms
コンパイル使用メモリ 160,816 KB
実行使用メモリ 5,212 KB
最終ジャッジ日時 2023-10-21 16:58:42
合計ジャッジ時間 3,628 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

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

import Data.Char

main = do
 getLine
 rs <- words <$> getLine
 putStrLn $ maybe "ERROR" id $ roman $ sum $ map arabic rs

roman n
 | n > 3999 = Nothing
 | otherwise = Just $ concat $ zipWith readArabic [1000,100,10,1] (digits n)
 where
  digits = reverse . take 4 . reverse . ([0,0,0] ++) . map digitToInt . show
  readArabic 1000 d
   | d == 0 = ""
   | otherwise = replicate d 'M'
  readArabic 100 d
   | d == 0 = ""
   | d <= 3 = replicate d 'C'
   | d == 4 = "CD"
   | d == 5 = "D"
   | d <= 8 = 'D' : replicate (d-5) 'C'
   | d == 9 = "CM"
  readArabic 10 d
   | d == 0 = ""
   | d <= 3 = replicate d 'X'
   | d == 4 = "XL"
   | d == 5 = "L"
   | d <= 8 = 'L' : replicate (d-5) 'X'
   | d == 9 = "XC"
  readArabic 1 d
   | d == 0 = ""
   | d <= 3 = replicate d 'I'
   | d == 4 = "IV"
   | d == 5 = "V"
   | d <= 8 = 'V' : replicate (d-5) 'I'
   | d == 9 = "IX"

arabic = sum . count . map readRoman
 where
  readRoman r = case r of {'I'->1; 'V'->5; 'X'->10; 'L'->50; 'C'->100; 'D'->500; 'M'->1000}
  count (x:y:xys) = if x<y then y-x : count xys else x : count (y:xys)
  count xs = xs
0