結果

問題 No.5 数字のブロック
ユーザー m0ntBL4Nc
提出日時 2019-09-01 17:51:40
言語 C
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,677 bytes
コンパイル時間 1,784 ms
コンパイル使用メモリ 29,568 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-28 20:19:38
合計ジャッジ時間 2,317 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 WA * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(void) {
    int boxWidth;
    int nBlock;

    // Get boxWidth and nBlock
    scanf("%d", &boxWidth);
    scanf("%d", &nBlock);

    int blockWidths[nBlock];
    int nBlockWidth = sizeof blockWidths / sizeof blockWidths[0];

    char buffer[100000];
    rewind(stdin);      // clear stdin
    fgets(buffer, 100000, stdin);

    char *p_buffer;
    p_buffer = strtok(buffer, " ");

    // Set blockWidths element
    int blockWidthsIndex = 0;
    while(p_buffer != NULL) {
        int blockWidth = atoi(p_buffer);
        blockWidths[blockWidthsIndex++] = blockWidth;
        p_buffer = strtok(NULL, " ");
    }

    // bubble-sort blockWidths (asc)
    int temp;
    int isChange = 1;      // if changed then isChange -> 1
    while(isChange == 1) {
        isChange = 0;
        for(int i=0; i<nBlockWidth-1; i++) {
            for(int j=i+1; j<nBlockWidth; j++) {
                if(blockWidths[i] > blockWidths[j]) {
                    temp = blockWidths[i];
                    blockWidths[i] = blockWidths[j];
                    blockWidths[j] = temp;
                    isChange = 1;
                }
            }
        }
    }

    // printf("\n");
    // for(int i=0; i<10; i++) {
    //     printf("%d\n", blockWidths[i]);
    // }
    // printf("%d\n", nBlockWidth);

    // put block in box
    int nBlockCanIntoBox = 0;
    int sumWidth = 0;
    for(int i=0; i<nBlockWidth; i++) {
        sumWidth += blockWidths[i];
        if(sumWidth > boxWidth) {
            break;
        } else {
            nBlockCanIntoBox++;
        }
    }

    printf("%d\n", nBlockCanIntoBox);
    return 0;
}
0