結果

問題 No.5 数字のブロック
ユーザー TwinkleStar74
提出日時 2017-09-13 19:40:00
言語 Java
(openjdk 23)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 1,086 bytes
コンパイル時間 1,912 ms
コンパイル使用メモリ 74,444 KB
実行使用メモリ 59,180 KB
最終ジャッジ日時 2024-11-18 11:42:16
合計ジャッジ時間 8,703 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.Arrays;

class No5{
       public static void main(String[] args){
              Scanner sc = new Scanner(System.in);
              int hw= sc.nextInt();  
              int n = sc.nextInt();
              int bw[] = new int[n];

              for ( int i=0; i<n; i++ ){   //配列bw[n]に入れていく
                    bw[i] = sc.nextInt();
              }

              Arrays.sort(bw);   //昇順にソート

              int totalBw= 0;
              int count =0;
              for ( int k=0; k<n; k++ ){   //変数にbw[0]から足していく
                    if ( totalBw < hw ){
                    totalBw += bw[k];
                    count++;
                    }
              }

             if ( totalBw < hw || totalBw == hw ){  //最後に足して箱の幅一致ならそのまま出力
             System.out.println ( count );
             }
             if ( totalBw > hw ){   //最後に足して箱の幅を越えれば-1して出力
             System.out.println ( count - 1 );
             }

       }
}
0