結果

問題 No.275 中央値を求めよ
ユーザー Naoki00712
提出日時 2023-05-30 16:48:58
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 640 bytes
コンパイル時間 333 ms
コンパイル使用メモリ 31,744 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-28 11:59:07
合計ジャッジ時間 1,524 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int cmp(const void* n1, const void* n2)
{
	if (*(int*)n1 > *(int*)n2)
	{
		return 1;
	}
	else if (*(int*)n1 < *(int*)n2)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int main()
{
	int  n;
	scanf("%d", &n);

	int* a;
	a = (int*)malloc(sizeof(int) * n);


	float ans;
	for (int i = 0; i < n; i++) 
	{
		scanf("%d", &a[i]);
	}

	if (a) 
	{
		qsort(a, n, sizeof(int), cmp);
	}

	if (n % 2 == 1)
	{
		ans = a[((n + 1) / 2) - 1];
		printf("%f", ans);
	}
	else
	{
		ans = roundf((((float)a[(n / 2) - 1] + (float)a[(n / 2)]) / 2) * 10) / 10;
		printf("%f", ans);
	}

	free(a);
	return 0;
}
0