結果

問題 No.275 中央値を求めよ
ユーザー nopperi
提出日時 2015-09-04 22:33:30
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 636 bytes
コンパイル時間 636 ms
コンパイル使用メモリ 55,860 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-24 23:08:58
合計ジャッジ時間 1,717 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdlib.h>
using namespace std;

int compare(const void* p1, const void* p2) {
    const int* n1 = (int*)p1;
    const int* n2 = (int*)p2;
    if (*n1 < *n2) {
        return -1;
    } else if (*n1 == *n2) {
        return 0;
    } else {
        return 1;
    }
}

int main(void) {
    int n;
    int list[1000] = {0};
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> list[i];
    }
    qsort(list, n, sizeof(int), compare);
    int mid = n/2;
    if (0 == (n % 2)) {
        cout << (list[mid-1] + list[mid])/2. << endl;
    } else {
        cout << list[mid] << endl;
    }
    return 0;
}
0