結果

問題 No.275 中央値を求めよ
コンテスト
ユーザー Kato Shinya
提出日時 2017-03-31 19:11:44
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 18 ms / 1,000 ms
コード長 938 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 173 ms
コンパイル使用メモリ 39,620 KB
最終ジャッジ日時 2026-02-21 23:35:20
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>
#include <stdbool.h>

void store_nums(float *p, int);
void bubble_sort(float *p, int);
void swap(float *xp, float *yp);
void print_median(float *p, int);

int main(void)
{
  int n;
  float nums[2000];

  scanf("%d\n", &n);

  store_nums(nums, n);
  bubble_sort(nums, n);
  print_median(nums, n);

  return 0;
}

void store_nums(float *arr, int n)
{
  for (int i = 0; i < n; ++i) {
    scanf("%f", &arr[i]);
  }
}

void bubble_sort(float *arr, int n)
{
  bool swapped;

  for (int i = 0; i < n-1; ++i) {
    swapped = false;
    for (int j = 0; j < n-i-1; ++j) {
      if (arr[j] > arr[j+1]) {
        swapped = true;
        swap(&arr[j], &arr[j+1]);
      }
    }
    if (swapped == false) {
      break;
    }
  }
}

void swap(float *xp, float *yp)
{
  float temp = *xp;
  *xp = *yp;
  *yp = temp;
}

void print_median(float *arr, int n)
{
  printf("%f\n", (n % 2 == 0) ? ((arr[n/2] + arr[n/2-1]) / 2) : arr[n/2]);
}
0