結果

問題 No.275 中央値を求めよ
ユーザー Takuya ItoTakuya Ito
提出日時 2022-12-10 13:32:36
言語 TypeScript
(4.6.2)
結果
AC  
実行時間 94 ms / 1,000 ms
コード長 912 bytes
コンパイル時間 7,219 ms
使用メモリ 36,108 KB
最終ジャッジ日時 2022-12-10 13:32:50
合計ジャッジ時間 11,914 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 69 ms
35,956 KB
testcase_01 AC 75 ms
35,844 KB
testcase_02 AC 71 ms
35,844 KB
testcase_03 AC 69 ms
35,784 KB
testcase_04 AC 71 ms
35,832 KB
testcase_05 AC 71 ms
35,828 KB
testcase_06 AC 70 ms
35,840 KB
testcase_07 AC 69 ms
35,820 KB
testcase_08 AC 69 ms
35,720 KB
testcase_09 AC 68 ms
35,868 KB
testcase_10 AC 70 ms
35,872 KB
testcase_11 AC 71 ms
35,696 KB
testcase_12 AC 70 ms
35,828 KB
testcase_13 AC 71 ms
35,868 KB
testcase_14 AC 75 ms
35,808 KB
testcase_15 AC 80 ms
35,992 KB
testcase_16 AC 72 ms
35,888 KB
testcase_17 AC 70 ms
35,904 KB
testcase_18 AC 72 ms
36,000 KB
testcase_19 AC 72 ms
35,796 KB
testcase_20 AC 72 ms
35,752 KB
testcase_21 AC 72 ms
35,988 KB
testcase_22 AC 71 ms
36,008 KB
testcase_23 AC 72 ms
35,792 KB
testcase_24 AC 71 ms
35,880 KB
testcase_25 AC 73 ms
36,108 KB
testcase_26 AC 71 ms
35,780 KB
testcase_27 AC 71 ms
35,752 KB
testcase_28 AC 70 ms
35,724 KB
testcase_29 AC 72 ms
35,728 KB
testcase_30 AC 71 ms
35,764 KB
testcase_31 AC 69 ms
35,768 KB
testcase_32 AC 70 ms
35,880 KB
testcase_33 AC 72 ms
35,920 KB
testcase_34 AC 60 ms
35,836 KB
testcase_35 AC 70 ms
35,804 KB
testcase_36 AC 94 ms
35,736 KB
testcase_37 AC 70 ms
35,768 KB
testcase_38 AC 70 ms
35,772 KB
testcase_39 AC 70 ms
35,760 KB
testcase_40 AC 71 ms
35,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

function Main(input: string): void {
  // 入力(2行)を受け取り、扱いやすいように処理
  const inputArr = input.split('\n');
  const numCount = parseInt(inputArr[0], 10);
  const nums = inputArr[1].split(' ')
                          .map(str => parseInt(str, 10))
                          .sort((a, b) => a - b);

  // 数値の数が偶数なら、中央に近い2つの値(length/2 番目とその1つ後ろ)の平均
  //         奇数なら、中央値の切り下げ 番目の値
  let median = 0;
  if (numCount % 2 == 0) {
    median = (nums[(numCount / 2) - 1] + nums[numCount / 2]) / 2
  } else {
    median = nums[Math.floor(numCount / 2)];
  }

  // 少数第一位までにする(10倍した数で四捨五入して、それを1/10して少数桁を1つだけ作る)
  console.log(Math.round(median * 10) / 10);
}

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