結果

問題 No.275 中央値を求めよ
ユーザー 3qvwn
提出日時 2019-07-03 22:25:26
言語 Elixir
(1.18.1)
結果
WA  
実行時間 -
コード長 732 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 62,448 KB
実行使用メモリ 58,696 KB
最終ジャッジ日時 2024-12-31 03:13:48
合計ジャッジ時間 29,351 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other AC * 2 WA * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

defmodule Main do
  def main do
    sorted_list = IO.gets("") |> String.trim |> String.split |> Enum.map(&String.to_integer/1) |> Enum.sort 
    index = median_index(sorted_list)
    median(sorted_list, index) |> IO.puts
  end
  
  def median(sorted_list, index) when is_integer(index) do
    sorted_list |> Enum.at(index)
  end
  
  def median(sorted_list, index) when is_list(index) do
    index |> Enum.map(&Enum.at(sorted_list, &1)) |> Enum.sum |> (fn sum -> sum/2 end).()
  end
  
  def median_index(sorted_list) do
    list_count = sorted_list |> Enum.count
    case list_count |> rem(2) do
      0 -> list_count/2 - 0.5 |> (fn hoge -> [floor(hoge), ceil(hoge)] end).()
      1 -> list_count/2 - 0.5 |> ceil
    end
  end
end
0