結果

問題 No.275 中央値を求めよ
ユーザー subsnsubsn
提出日時 2023-05-29 12:38:08
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,982 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 30,244 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-27 23:40:14
合計ジャッジ時間 1,491 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,380 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,504 KB
testcase_05 AC 0 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 0 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <malloc.h>
char str[60000];
int str_len = 0;

/// <summary>
/// グローバル変数strの中身を入力された文字列で上書きする
/// </summary>
void ReadString() {
	char c = getchar();
	str_len = 0;

	while (c != '\n') {
		str[str_len] = c;
		c = getchar();
		str_len++;
	}
}

/// <summary>
/// 入力された数字を返す
/// </summary>
/// <returns></returns>
int ReadNum() {
	char c = getchar();
	int num = 0;
	int numCnt = 0;
	while (c != '\n') {
		num = num * 10 + c - '0';
		c = getchar();
	}

	return num;
}

/// <summary>
/// 引数numsの中の値を昇順にソートする
/// </summary>
/// <param name="nums">ソートしたい配列</param>
/// <param name="n">ソートしたい配列の要素数</param>
void Sort(int* nums, int n) {
	for (int i = 0;i < n - 1;i++) {
		for (int j = i + 1;j < n;j++) {
			if (nums[i] > nums[j]) {
				int work = nums[i];
				nums[i] = nums[j];
				nums[j] = work;
			}
		}
	}
}

/// <summary>
/// 引数*numsのポインタ位置にある配列の中身をstrのスペースで区切られた数字にする
/// 渡されたポインタ位置が、strのスペースの数+1個分 静的にメモリが確保された配列である必要がある
/// </summary>
/// <param name="nums"></param>
/// <returns></returns>
void GetNums(int* nums, int size) {
	for (int i = 0;i < size;i++) {
		nums[i] = 0;
	}
	int numIndex = 0;
	int negate = 0;
	for (int i = 0;i <= str_len;i++) {
		if (str[i] == ' ' || i == str_len) {
			if (negate == 1) {
				nums[numIndex] *= -1;
			}
			numIndex++;
			continue;
		}
		if (str[i] == '-') {
			negate = 1;
			continue;
		}
		nums[numIndex] = nums[numIndex] * 10 + (str[i] - '0');
	}
}


int main()
{
	int n = ReadNum();
	int* nums;
	nums = (int*)malloc(sizeof(int) * n);
	ReadString();
	GetNums(nums,n);
	Sort(nums, n);
	if (n % 2 == 1) {
		printf("%d\n" , nums[n / 2]);
	}
	else {
		printf("%.1f\n",((float)nums[n / 2 - 1] + (float)nums[n / 2])/2);
	}
}
0