結果

問題 No.275 中央値を求めよ
ユーザー matsuyoshi30matsuyoshi30
提出日時 2016-01-01 19:54:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 213 ms / 1,000 ms
コード長 1,167 bytes
コンパイル時間 2,288 ms
コンパイル使用メモリ 74,124 KB
実行使用メモリ 57,728 KB
最終ジャッジ日時 2023-09-07 05:08:08
合計ジャッジ時間 10,968 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
56,048 KB
testcase_01 AC 130 ms
56,004 KB
testcase_02 AC 147 ms
55,720 KB
testcase_03 AC 130 ms
55,868 KB
testcase_04 AC 134 ms
55,768 KB
testcase_05 AC 129 ms
55,832 KB
testcase_06 AC 139 ms
57,728 KB
testcase_07 AC 202 ms
56,164 KB
testcase_08 AC 149 ms
55,776 KB
testcase_09 AC 149 ms
55,508 KB
testcase_10 AC 150 ms
56,080 KB
testcase_11 AC 129 ms
55,844 KB
testcase_12 AC 135 ms
55,908 KB
testcase_13 AC 131 ms
55,748 KB
testcase_14 AC 129 ms
55,736 KB
testcase_15 AC 209 ms
56,508 KB
testcase_16 AC 204 ms
56,012 KB
testcase_17 AC 213 ms
56,872 KB
testcase_18 AC 168 ms
55,928 KB
testcase_19 AC 207 ms
56,104 KB
testcase_20 AC 200 ms
56,352 KB
testcase_21 AC 207 ms
55,952 KB
testcase_22 AC 207 ms
56,580 KB
testcase_23 AC 189 ms
55,724 KB
testcase_24 AC 163 ms
55,752 KB
testcase_25 AC 201 ms
56,540 KB
testcase_26 AC 201 ms
56,492 KB
testcase_27 AC 210 ms
56,052 KB
testcase_28 AC 146 ms
55,488 KB
testcase_29 AC 165 ms
53,948 KB
testcase_30 AC 139 ms
56,084 KB
testcase_31 AC 205 ms
55,904 KB
testcase_32 AC 157 ms
55,800 KB
testcase_33 AC 208 ms
56,616 KB
testcase_34 AC 151 ms
55,756 KB
testcase_35 AC 181 ms
55,856 KB
testcase_36 AC 183 ms
55,984 KB
testcase_37 AC 154 ms
55,944 KB
testcase_38 AC 159 ms
55,872 KB
testcase_39 AC 165 ms
56,100 KB
testcase_40 AC 206 ms
56,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//275 median

/*
median
array -> sort -> average between 2 number in array's central
*/

import java.util.Scanner;

class Median {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arrayInt = new int[n];
        //順番に配列に挿入
        for(int i = 0; i < arrayInt.length; i++) {
            arrayInt[i] = in.nextInt();
        }
        //数の大きい順にソート
        for(int i = 0; i < arrayInt.length - 1; i++) {
            for(int j = arrayInt.length - 1; j > i; j--) {
                if(arrayInt[j] < arrayInt[j - 1]) {
                    int t = arrayInt[j];
                    arrayInt[j] = arrayInt[j - 1];
                    arrayInt[j - 1] = t;
                }
            }
        }
        if(n % 2 == 1) {
            int b = (n + 1) / 2;
            System.out.println(arrayInt[b - 1]);
        } else {
            double b = ((n + 1.0) / 2.0);
            int c = (int)((b - 0.5) - 1);
            int d = (int)((b + 0.5) - 1);
            double e = (arrayInt[c] + arrayInt[d]) / 2.0;
            System.out.println(e);
        }
    }
}
0