2012-08-01から1ヶ月間の記事一覧

Codeforces 3A - A. Shortest path of the king

3A - A. Shortest path of the king import Data.Char parse :: String -> (Int, Int) parse s = (digitToInt (s!!1), ord (s!!0) - ord 'a' + 1) calc :: (Int, Int) -> (Int, Int) -> [String] calc (sr, sc) (dr, dc) | sr == dr && sc == dc = [] | othe…

Codeforces 139A - A. Petr and Book

139A - A. Petr and Book calc :: Int -> [Int] -> Int calc n pages = calc' n (cycle pages) (cycle [1..7]) where calc' n (p:pages) (d:dayWeek) | n <= p = d | otherwise = calc' (n-p) pages dayWeek main = do s <- getLine t <- getLine print $ ca…

Codeforces 146A - A. Lucky Ticket

146A - A. Lucky Ticket import Data.Char isLucky :: String -> Bool isLucky s = all (\c -> c == '4' || c == '7') s calc :: String -> String calc s = if isLucky s && sum hd == sum tl then "YES" else "NO" where xs = map digitToInt s n = length…

Codeforces 219A - A. k-String

219A - A. k-String 文字列をソートする。 同じ文字でグルーピング。 グルーピングした文字列の長さが k の倍数なら、k-string は作れる。そうでなければ k-string は作れない import Data.List calc :: Int -> String -> Maybe String calc k s = if all (\…

Codeforces 50A - A. Domino piling

50A - A. Domino pilingM x N サイズのボードに 2 x 1 サイズのドミノを出来るだけ沢山敷き詰める問題。ボードに配置可能なドミノの個数を出力する。制約: 1 ボードのサイズが偶数であれば、隙間なくドミノを敷き詰められます。なので、まず偶数サイズのボー…

Codeforces 189A - A. Cut Ribbon

189A - A. Cut Ribbon長さ n のリボンある。そのリボンを出来るだけたくさんカットしたい。カットされたリボンの長さは、a, b, cのいずれかでなければならない。最大でいくつのリボンにカットできるか、という問題。制約: 1 DPの問題なのでC++で解きました。…

Codeforces 4A - A. Watermelon

4A - A. Watermelon calc :: Int -> String calc n | n > 2 && even n = "YES" | otherwise = "NO" main = do s <- getLine putStrLn $ calc $ read s

Haskellのdoの中のif式のインデント

Haskellについて調べていたら、do の中の if 式の then と else は if よりもインデントを深くする必要があるという情報があった。手元で試す限り、if, then, elseを揃えても特にエラーにならない。仕様が変わったのだろうか。もしかしたらと思い、-Wall を…

Codeforces 208A - A. Dubstep

208A - A. Dubstep WUBを空白に置き換える 連続する空白をひとつの空白にまとめる 両端の空白を取り除く -- "WUB"を取り除く(代わりに空白をおく) removeWUB :: String -> String removeWUB "" = "" removeWUB ('W':'U':'B':xs) = ' ' : removeWUB xs remove…

Codeforces 1A - A. Theatre Square

1A - A. Theatre Square calc n m a = x * y where x = (n `div` a) + (signum $ n `mod` a) y = (m `div` a) + (signum $ m `mod` a) main = do s <- getLine let [n, m, a] = map read $ words s :: [Integer] print $ calc n m a signumを括弧で囲まない…

Codeforces 216A - A. Tiling with Hexagons

216A - Tiling with Hexagonsb*cを一つの固まりとみなす。aの値が増減するとき、b*cの固まりを取り除いた図形のパターンがどのように変換するかを考えた。b*cの固まりを取り除くと、以下の「へ」の字のラインが見えてくる。 calc a b c = (a-1) * (b + c - 1…

fizzbuzz

http://chaton.practical-scheme.net/gauche/a/2012/08/09#entry-5023d040-7e774 のPythonコードが分かりやすかったので、Haskellで書いてみました。 fizz = cycle ["", "", "fizz"] buzz = cycle ["", "", "", "", "buzz"] fizzbuzz n = [if xy == "" then …

Codeforces 194B - B. Square

194B - B. Square 左下隅からスタートして、はじめて4隅に止まるのは何ステップ目だろうか。 小さいサイズで試してみる。 正方形のサイズを n とすると、 n = 1 のときは、1 ステップ目で四隅にはじめて到達する n = 2 のときは、2 ステップ目 n = 3 のとき…

Codeforces 205A - Little Elephant and Rozdil

205A - Little Elephant and Rozdil以下のように書いたら、calcに渡すリストのサイズが 1 のときにRUNTIME_ERRORとなりました。 import Data.List calc :: [Int] -> String calc xs | length xs > 1 && a == b = "Still Rozdil" | otherwise = case findInde…

Codeforces 214A - A. System of Equations

214A - A. System of Equations全探索。 calc n m = sum [1 | a<-[0..1000], b<-[0..1000], a^2+b==n, a+b^2==m] main = do s <- getLine let xs = map (\x -> read x :: Int) $ words s let (n, m) = (xs!!0, xs!!1) putStrLn $ show $ calc n m

Codeforces 200B - Drinks

200B - Drinks calc xs = (sum xs / fromIntegral (100 * length xs)) * 100 main = do s <- getLine t <- getLine putStrLn $ show $ calc $ map (\x -> read x :: Double) $ words t 型に関する理解があいまいなので、型エラーがなかなか取れませんでした…

Codeforces 199A - A. Hexadecimal's theorem

199A - A. Hexadecimal's theoremフィボナッチ数列を求める関数を作成しましたが、必要ありませんでした。 main = do s <- getLine putStrLn ("0 0 " ++ s)

Codeforcesの問題をHaskellで解いてみる

Haskellで問題を解いてみます。CodeforcesのRatingは1300くらいなので、div2の易しい問題を中心に解いていきたいと思います。