結果
問題 | No.5 数字のブロック |
ユーザー |
|
提出日時 | 2022-12-13 17:27:39 |
言語 | TypeScript (5.7.2) |
結果 |
AC
|
実行時間 | 87 ms / 5,000 ms |
コード長 | 991 bytes |
コンパイル時間 | 8,404 ms |
コンパイル使用メモリ | 229,272 KB |
実行使用メモリ | 45,952 KB |
最終ジャッジ日時 | 2024-12-31 16:48:15 |
合計ジャッジ時間 | 11,979 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 34 |
ソースコード
function Main(input: string): void { const inputArr: string[] = input.split('\n'); const boxWidth: number = parseInt(inputArr[0]); const blocksWidth: number[] = inputArr[2].split(' ') .map(str => parseInt(str)) .sort((a, b) => a - b); let sum = 0; // 何個入るか をindexを使って表現したいが、ループ途中でbreakもしたいのでArray.forEach()が使えない // そのため、 for of + Object.entries()を採用 for (const [index, blockWidth] of Object.entries(blocksWidth)) { // (途中のブロックや最後のブロックが)箱に収まらなかったとき or 最後のブロックが収まったとき // 箱に収まるブロック数を出力 sum += blockWidth; if (sum > boxWidth) { console.log(parseInt(index)); break; } if (parseInt(index) + 1 == blocksWidth.length) { console.log(blocksWidth.length); break; } } } Main(require("fs").readFileSync("/dev/stdin", "utf8"));