結果
| 問題 |
No.275 中央値を求めよ
|
| コンテスト | |
| ユーザー |
piconic_X
|
| 提出日時 | 2015-11-12 03:19:18 |
| 言語 | OCaml (5.2.1) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 1,000 ms |
| コード長 | 1,029 bytes |
| コンパイル時間 | 409 ms |
| コンパイル使用メモリ | 19,700 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-08 23:39:55 |
| 合計ジャッジ時間 | 1,231 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 38 |
ソースコード
let string_to_list str n cut =
if n = 1 then [int_of_string str]
else
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"
piconic_X