結果

問題 No.275 中央値を求めよ
ユーザー piconic_X
提出日時 2015-11-12 03:15:11
言語 OCaml
(5.2.1)
結果
RE  
実行時間 -
コード長 1,008 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 19,828 KB
実行使用メモリ 6,816 KB
最終ジャッジ日時 2024-10-08 23:39:40
合計ジャッジ時間 1,786 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37 RE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

let string_to_list str n cut =
  let rec move_element pos ls =
    if List.length ls = n - 1 then
      let last_int = int_of_string (String.sub str (pos + 1) ((String.length str - 1) - pos)) in
      last_int :: ls
    else
      let pos1 = String.index_from str (pos + 1) cut in
      let strint = String.sub str (pos + 1) (pos1 - pos - 1) in
      let int = int_of_string strint in
      let newls = int :: ls in
      move_element pos1 newls
  in
  let first_pos = String.index_from str 0 cut in
  let first_int = int_of_string (String.sub str 0 first_pos) in
  move_element first_pos [first_int]

let median n ls =
  let ls_sorted = List.fast_sort compare ls in
  if n mod 2 = 0 then
    (float_of_int ((List.nth ls_sorted ((n/2) - 1)) + (List.nth ls_sorted ((n/2 + 1) - 1)))) /. 2.
  else
    float_of_int (List.nth ls_sorted (((n + 1) / 2) - 1))


let () =
  let n = int_of_string (read_line ()) in
  let args = read_line () in
  print_float (median n (string_to_list args n ' '));
  print_string "\n"
0