結果

問題 No.275 中央値を求めよ
コンテスト
ユーザー piconic_X
提出日時 2015-11-12 03:15:11
言語 OCaml
(5.5.0)
コンパイル:
ocamlfind ocamlopt -linkpkg -package zarith,str _filename_ -o a.out
実行:
./a.out
結果
RE  
実行時間 -
コード長 1,008 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 109 ms
コンパイル使用メモリ 21,632 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2026-09-12 07:00:50
合計ジャッジ時間 2,187 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37 RE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
ocamlfind: [WARNING] Cannot read directory ./stublibs which is mentioned in ld.conf

ソースコード

diff #
raw source code

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