結果

問題 No.9002 FizzBuzz(テスト用)
ユーザー your_nameyour_name
提出日時 2016-11-17 19:41:10
言語 Haskell
(9.8.2)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,475 bytes
コンパイル時間 7,907 ms
コンパイル使用メモリ 168,960 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-04 14:57:52
合計ジャッジ時間 8,297 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 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 #

data FizzBuzz =
  Numero Int
  | Fizz
  | Buzz
  | FizzBuzz

instance Show FizzBuzz where
  show (Numero x) = show x
  show Fizz       = "Fizz"
  show Buzz       = "Buzz"
  show FizzBuzz   = "FizzBuzz"

type Max = Int
type Current = Int

data Counter = Counter {
  max :: Max
  , current :: Current
  } deriving (Show, Eq)

class Next n where
  next :: n -> n

counter :: Max -> Counter
counter m = Counter m 1

instance Next Counter where
  next ct@(Counter m c)
    | match ct  = Counter m 1
    | otherwise = Counter m (c + 1)

match :: Counter -> Bool
match (Counter m c)
  | m == c    = True
  | otherwise = False

data Runner = Runner Counter Counter Counter

instance Next Runner where
  next (Runner i f b) = Runner (next i) (next f) (next b)

runner :: Int -> Runner
runner m = Runner (counter m) (counter 3) (counter 5)

fin :: Runner -> Bool
fin (Runner i _ _) = match i

run :: Runner -> [FizzBuzz]
run r
  | fin r     = [fizzbuzz r]
  | otherwise = (fizzbuzz r) : (run $ next r)

fizzbuzz :: Runner -> FizzBuzz
fizzbuzz (Runner i f b) = case (match f, match b) of
  (True, True)   -> FizzBuzz
  (False, True)  -> Buzz
  (True, False)  -> Fizz
  (_, _)         -> Numero $ current i

readInput :: IO String -> IO Int
readInput io = fmap read io

showResult :: [FizzBuzz] -> IO ()
showResult = mapM_ print

app :: IO String -> IO ()
app io = do
  s <- readInput io
  r <- return $ runner s
  r' <- return $ run r
  showResult r'

main :: IO ()
main = app getLine
0