結果

問題 No.275 中央値を求めよ
ユーザー GBGB
提出日時 2018-04-08 16:08:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 191 ms / 1,000 ms
コード長 940 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 74,964 KB
実行使用メモリ 56,824 KB
最終ジャッジ日時 2023-09-07 06:52:05
合計ジャッジ時間 10,149 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,600 KB
testcase_01 AC 126 ms
55,932 KB
testcase_02 AC 135 ms
55,920 KB
testcase_03 AC 121 ms
55,852 KB
testcase_04 AC 123 ms
55,744 KB
testcase_05 AC 129 ms
55,696 KB
testcase_06 AC 137 ms
55,964 KB
testcase_07 AC 185 ms
54,284 KB
testcase_08 AC 132 ms
55,804 KB
testcase_09 AC 129 ms
55,676 KB
testcase_10 AC 134 ms
55,528 KB
testcase_11 AC 121 ms
55,840 KB
testcase_12 AC 124 ms
55,740 KB
testcase_13 AC 121 ms
55,640 KB
testcase_14 AC 124 ms
56,104 KB
testcase_15 AC 191 ms
56,824 KB
testcase_16 AC 191 ms
56,484 KB
testcase_17 AC 187 ms
56,136 KB
testcase_18 AC 149 ms
55,832 KB
testcase_19 AC 174 ms
56,020 KB
testcase_20 AC 189 ms
56,724 KB
testcase_21 AC 153 ms
56,256 KB
testcase_22 AC 183 ms
56,372 KB
testcase_23 AC 172 ms
55,960 KB
testcase_24 AC 142 ms
55,752 KB
testcase_25 AC 185 ms
56,552 KB
testcase_26 AC 180 ms
54,180 KB
testcase_27 AC 182 ms
56,420 KB
testcase_28 AC 127 ms
55,756 KB
testcase_29 AC 147 ms
55,756 KB
testcase_30 AC 128 ms
55,932 KB
testcase_31 AC 182 ms
56,440 KB
testcase_32 AC 130 ms
55,560 KB
testcase_33 AC 175 ms
56,076 KB
testcase_34 AC 126 ms
55,916 KB
testcase_35 AC 178 ms
56,728 KB
testcase_36 AC 149 ms
55,640 KB
testcase_37 AC 134 ms
55,592 KB
testcase_38 AC 140 ms
55,764 KB
testcase_39 AC 142 ms
55,916 KB
testcase_40 AC 176 ms
56,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main{
  public static void main(String[] args)throws IOException {

    //nの入力,配列arrayの入力
    Scanner scan=new Scanner(System.in);
    int n=Integer.parseInt(scan.next());
    int[] array=new int[n];
    for(int i=0;i<array.length;i++){
      array[i]=Integer.parseInt(scan.next());
    }
    float median=0;
    //ソート
    for(int i=0;i<array.length-1;i++){
      for(int j=array.length-1;j>i;j--){
        if(array[j]<array[j-1]){
          int tmp=array[j];
          array[j]=array[j-1];
          array[j-1]=tmp;
        }
      }
    }

    //nが偶数であればn/2と(n/2)-1の配列の値の平均を取る.nが奇数であればn-1/2番目の値を選ぶ
    if(n%2==0){
      median=(float)(array[n/2]+array[n/2-1])/2;
      System.out.println(String.format("%.1f",median));
    }else{
      median=array[(n-1)/2];
      System.out.println(median);
    }
  }
}
0