結果

問題 No.275 中央値を求めよ
ユーザー momen999
提出日時 2017-04-12 22:35:11
言語 Haskell
(9.10.1)
結果
AC  
実行時間 11 ms / 1,000 ms
コード長 803 bytes
コンパイル時間 5,430 ms
コンパイル使用メモリ 173,568 KB
実行使用メモリ 8,576 KB
最終ジャッジ日時 2024-06-25 00:28:17
合計ジャッジ時間 6,538 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 #

bswap [x] = [x]
bswap (x:y:zs)
    | x < y     = y : bswap (x:zs)
    | otherwise = x : bswap (y:zs)

bsort [] = []
bsort xs = y : bsort ys
    where
        (y:ys) = reverse $ bswap xs

median :: [Int] -> Int -> Double
median [x] _ = fromIntegral (x :: Int) :: Double
median l n   | n `mod` 2 == 0   = l_even (l !! n') (l !! (n' + 1))
             | otherwise        = fromIntegral (l !! n' :: Int) :: Double
    where
        n' = floor $ (fromIntegral (n - 1 :: Int)) / 2.0    -- 小数点以下切り捨て 3.5 -> 3
        l_even x x' = (fromIntegral ((x + x') :: Int)) / 2.0 :: Double

main = do
    str1 <- getLine
    str2 <- getLine
    let
        w = words str2
        n = read str1
        l = map read w :: [Int]
        bl = bsort l :: [Int]
        result = median bl n
    print result
0