結果
| 問題 | 
                            No.518 ローマ数字の和
                             | 
                    
| コンテスト | |
| ユーザー | 
                             aimy
                         | 
                    
| 提出日時 | 2017-05-29 22:37:21 | 
| 言語 | Haskell  (9.10.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,089 bytes | 
| コンパイル時間 | 3,210 ms | 
| コンパイル使用メモリ | 173,312 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-09-21 18:12:43 | 
| 合計ジャッジ時間 | 4,063 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
コンパイルメッセージ
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
ソースコード
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
            
            
            
        
            
aimy