LoginSignup
5
1

More than 5 years have passed since last update.

ABC093 in Haskell

Last updated at Posted at 2018-04-07

HaskellでAtCoder Beginner Contest 093を解いてみました。

A

そのまま。

a.hs
import Data.List

main = do
  str <- getLine
  putStrLn $ if elem str $ permutations "abc" then "Yes" else "No"

と思ったら条件より以下の方が適切でした。

a.hs
import Data.List

main = do
  str <- getLine
  putStrLn $ if nub str == str then "Yes" else "No"

B

そのまま。

b.hs
import Data.List

solve :: Int -> Int -> Int -> [Int]
solve a b k =
  if b-a+1 > 2*k then [a..a+k-1] ++ [(b-k+1)..b]
  else [a..b]

main = do
  [a,b,k] <- map read . words <$> getLine :: IO [Int]
  mapM print (solve a b k)

C

case部分が汚いが一応通る。もう少しスマートに書きたい。

c.hs
import Data.List

solve :: Int -> Int -> Int -> Int
solve a b c =
  (div (z - x) 2) + (div (z - y) 2) + 
   case ((z - x) `mod` 2) + ((z - y) `mod`2) of
     0 -> 0
     1 -> 2
     2 -> 1
  where
    [x,y,z] = sort [a,b,c]


main = do
  [a,b,c] <- map read . words <$> getLine :: IO [Int]
  print $ solve a b c

D

これで動かないことも知ってるしそもそもTLE...
あとで書き直します。

d.hs
import Control.Monad
import Data.List

solve :: [[Int]] -> [Int]
solve [] = []
solve ([a,b]:qs) =
  e : solve qs
  where
    maxInt = 10^10^10
    e = length  [(x,y) | x<-[1..(max a b)],y<-[1..(max a b)],x/=a&&y/=b&&x*y<a*b]


main = do
  q <- read <$> getLine ::IO Int
  ql <- replicateM q $ map read . words <$> getLine ::IO [[Int]]
  mapM print $ solve ql
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1