結果

問題 No.5 数字のブロック
ユーザー subsnsubsn
提出日時 2023-05-26 15:42:18
言語 C
(gcc 12.3.0)
結果
RE  
実行時間 -
コード長 2,118 bytes
コンパイル時間 691 ms
コンパイル使用メモリ 28,792 KB
実行使用メモリ 5,272 KB
最終ジャッジ日時 2023-08-26 07:20:37
合計ジャッジ時間 4,698 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#include <iostream>
#include <stdio.h>
#include <malloc.h>
char str[200];//(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