Codeforces 146B - B. Lucky Mask
整数 a, b が与えられたとき、c のマスクが b と等しく、かつ c > a であるような c を求めよという問題。
「c のマスクが b と等しい」とは、整数 c から 4 と 7 以外の数字を取り除いた数が b と等しいという意味。
制約:
1 <= a, b <= 10^5
全探索。
mask :: Int -> Int mask n | null s = 0 | otherwise = read s where s = filter (\c -> c == '4' || c == '7') $ show n calc :: Int -> Int -> Int calc a b = head [i | i <- [a+1..], b == mask i] main = do s <- getLine let [a, b] = map read $ words s :: [Int] print $ calc a b
はじめにmask関数を書いたとき、if式を用いていたけど、ガードに書き直した。ガードの方が読みやすく分かりやすい。
mask :: Int -> Int mask n = if null s then 0 else read s where s = filter (\c -> c == '4' || c == '7') $ show n