結果

問題 No.275 中央値を求めよ
ユーザー Takuya ItoTakuya Ito
提出日時 2022-12-10 13:15:05
言語 TypeScript
(4.6.2)
結果
WA  
実行時間 -
コード長 829 bytes
コンパイル時間 8,812 ms
使用メモリ 37,976 KB
最終ジャッジ日時 2022-12-10 13:15:19
合計ジャッジ時間 13,102 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 67 ms
35,880 KB
testcase_01 AC 70 ms
35,832 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 67 ms
35,876 KB
testcase_05 AC 68 ms
35,952 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 61 ms
35,776 KB
testcase_13 WA -
testcase_14 AC 67 ms
35,756 KB
testcase_15 WA -
testcase_16 AC 66 ms
35,932 KB
testcase_17 AC 83 ms
35,696 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

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();

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

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

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