結果

問題 No.5 数字のブロック
ユーザー subsnsubsn
提出日時 2023-05-26 15:45:51
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 202 ms / 5,000 ms
コード長 1,786 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 28,472 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-26 07:22:29
合計ジャッジ時間 3,574 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#include <iostream>
#include <stdio.h>
#include <malloc.h>
char str[60000];//(5桁×10000件)+(空白1万-1) = 59999文字まで使われる可能性がある
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;
}

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;
			}
		}
	}
}

int main()
{
	int boxWidth = ReadNum();	//箱のサイズ
	int quantity = ReadNum();	//箱に入れるブロックの数

	int *size;
	size = (int*)malloc(sizeof(int) * quantity);//箱に入れるブロックの数の分メモリーを動的に確保する


	ReadString();	//それぞれのブロックのサイズ
	int num = 0;	//集計中の値
	int cnt = 0;	//strの何番目を参照するかのカウンタ
	int index = 0;	//sizeの何番目に出来上がった値を入れるかのカウンタ
	while (1) {
		if (cnt >= str_len) {
			size[index] = num;
			break;
		}
		if (str[cnt] == ' ') {
			size[index] = num;
			num = 0;
			index++;
			cnt++;
			continue;
		}
		num = num * 10 + (str[cnt] - '0');
		cnt++;
	}
	Sort(size,quantity);
	int sum = 0;
	int canPlace = 0;
	for (int i = 0;i < quantity;i++) {
		sum += size[i];
		if (sum > boxWidth) {
			break;
		}
		canPlace++;
	}
	printf("%d\n",canPlace);
}
0