結果

問題 No.156 キャンディー・ボックス
ユーザー kachipankachipan
提出日時 2023-07-03 15:02:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,782 bytes
コンパイル時間 626 ms
コンパイル使用メモリ 29,736 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 11:40:46
合計ジャッジ時間 1,451 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

void QuickSort(int arr[], int start, int last);

int main()
{
	int candyBox = 0;
	int takeCandy = 0;
	int candy[10] = { 0 };
	int zero = 0;

	scanf("%d", &candyBox);
	scanf("%d", &takeCandy);

	for (int i = 0;i < candyBox;i++)
	{
		scanf("%d", &(candy[i]));
	}

	QuickSort(candy, 0, candyBox - 1);

	for (int i = 0;i < candyBox;i++)
	{
		takeCandy -= candy[i];
		if (takeCandy < 0)
		{
			zero = i;
			break;
		}
		else if (takeCandy == 0)
		{
			zero = i + 1;
			break;
		}
	}

	printf("%d\n", zero);

	return 0;
}

void QuickSort(int arr[], int start, int last)
{
	int i = start;
	int k = last;
	int standard = arr[start];		// 基準値

	// ソートを行う範囲が1以下の場合は抜ける
	if (start >= last)
	{
		return;
	}

	while (true)
	{
		// pivotより値が大きくなるまで要素番号が小さい順に配列を探索
		while (arr[i] < standard)
		{
			i++;
		}

		// pivotより値が小さくなるまで要素番号が大きい順に配列を探索
		while (arr[k] > standard)
		{
			k--;
		}

		// iがk以上になったら、基準の数値より小さい側と大きい側にわけられている
		if (i >= k)
		{
			break;
		}

		// ここまで来てたらarr[i]は基準値より大きく、arr[k]は基準値より小さい
		// ので値を交換する
		int tmp = arr[i];
		arr[i] = arr[k];
		arr[k] = tmp;

		// 探索を再開
		i++;
		k--;
	}

	// 基準値より小さい範囲に対して同じ処理を行う
	// arr[i]は基準値なので-1して渡す。
	QuickSort(arr, start, i - 1);

	// 基準値より大きい範囲に対して同じ処理を行う
	// arr[k]は基準値なので+1して渡す。
	QuickSort(arr, k + 1, last);
}
0