結果

問題 No.5 数字のブロック
ユーザー subsnsubsn
提出日時 2023-05-26 15:22:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,075 bytes
コンパイル時間 515 ms
コンパイル使用メモリ 69,316 KB
実行使用メモリ 11,940 KB
最終ジャッジ日時 2023-08-26 07:13:07
合計ジャッジ時間 13,872 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
char str[60000];//(5桁×10000件)+(空白1万-1) = 59999文字まで使われる可能性がある
int str_len = 0;

/// <summary>
/// グローバル変数strの中身を入力された文字列で上書きする
/// </summary>
void ReadString() {
	fseek(stdin, 0, SEEK_END);//入力のバッファをクリアする(しない場合getcharがバッファから文字を読み取り、入力待ちにならない)
	char c = getchar();
	str_len = 0;

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

/// <summary>
/// 入力された数字を返す
/// </summary>
/// <returns></returns>
int ReadNum() {
	fseek(stdin, 0, SEEK_END);//入力のバッファをクリアする(しない場合getcharがバッファから文字を読み取り、入力待ちにならない)
	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