結果

問題 No.275 中央値を求めよ
ユーザー f Oxf Ox
提出日時 2021-04-14 21:46:52
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 84 ms / 1,000 ms
コード長 807 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 42,524 KB
最終ジャッジ日時 2023-09-13 11:10:48
合計ジャッジ時間 5,992 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
42,196 KB
testcase_01 AC 78 ms
42,124 KB
testcase_02 AC 77 ms
42,160 KB
testcase_03 AC 76 ms
41,884 KB
testcase_04 AC 76 ms
41,820 KB
testcase_05 AC 76 ms
42,524 KB
testcase_06 AC 76 ms
41,840 KB
testcase_07 AC 77 ms
41,884 KB
testcase_08 AC 76 ms
41,836 KB
testcase_09 AC 76 ms
41,900 KB
testcase_10 AC 78 ms
42,100 KB
testcase_11 AC 76 ms
42,288 KB
testcase_12 AC 77 ms
41,952 KB
testcase_13 AC 77 ms
41,876 KB
testcase_14 AC 76 ms
41,748 KB
testcase_15 AC 79 ms
42,252 KB
testcase_16 AC 77 ms
41,960 KB
testcase_17 AC 76 ms
41,820 KB
testcase_18 AC 78 ms
42,220 KB
testcase_19 AC 77 ms
41,748 KB
testcase_20 AC 77 ms
41,968 KB
testcase_21 AC 77 ms
42,172 KB
testcase_22 AC 79 ms
42,236 KB
testcase_23 AC 78 ms
41,956 KB
testcase_24 AC 78 ms
42,092 KB
testcase_25 AC 78 ms
42,240 KB
testcase_26 AC 77 ms
41,948 KB
testcase_27 AC 77 ms
41,912 KB
testcase_28 AC 76 ms
41,932 KB
testcase_29 AC 77 ms
41,956 KB
testcase_30 AC 77 ms
41,932 KB
testcase_31 AC 76 ms
41,948 KB
testcase_32 AC 78 ms
41,776 KB
testcase_33 AC 79 ms
41,752 KB
testcase_34 AC 77 ms
42,112 KB
testcase_35 AC 81 ms
41,876 KB
testcase_36 AC 77 ms
41,896 KB
testcase_37 AC 76 ms
41,936 KB
testcase_38 AC 76 ms
42,092 KB
testcase_39 AC 77 ms
41,944 KB
testcase_40 AC 84 ms
42,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

function Main(input) {
  let data = input.split("\n");
  const N = Number(data[0]);
  let A = data[1].split(" ");
  let numsA = A.map(Number); // 配列を文字列から数値に変換する。同時にソート済み。
  let sortNumsA = numsA.sort((a,b)=>a-b);
  // ↑この上の行でmap(Number)をすることでソートされている。よって、ソート関数は不要。
  
  if (N<=1) {
      console.log(Number(A));
  } else if (N%2===1) { // 奇数なら丁度真ん中がメジアン
    console.log(sortNumsA[Math.floor((N/2)-1)+1]);
  } else if (N%2===0) { // 偶数なら間2つの平均値がメジアン
    let bellowM = sortNumsA[(N/2)-1];
    let upperM = sortNumsA[(N/2)];
    console.log((bellowM + upperM)/2);
  } 



}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
0