結果

問題 No.275 中央値を求めよ
ユーザー hape8810
提出日時 2019-01-08 13:25:16
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 725 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 24,192 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-24 00:41:00
合計ジャッジ時間 1,505 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
main.cpp:37:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |         scanf("%d", a + i);
      |         ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>

void swap(int *n1, int *n2) {
    int tmp = *n1;
    *n1 = *n2;
    *n2 = tmp;
}
    
void sort(int a[], int N) {
    int i, j;
    
    for (i = 0; i < N - 1; i++) {
        for (j = N - 1; j > i; j--) {
            if (a[j - 1] > a[j]) {
                swap(&a[j - 1], &a[j]);
            }
        }
    }
}

double median (int a[], int N) {
    if (N % 2 == 1) {
        return (double)a[N/2];
    }
    else {
        return (double)(a[N/2 - 1] + a[N/2]) / 2.0;
    }
}

int main(void) {
    int N;
    int a[1000];
    int i;
    
    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d", a + i);
    }
    
    sort(a, N);
    
    printf("%.1f\n", median(a, N));
    
    return 0;
}
0