結果

問題 No.5 数字のブロック
ユーザー yo-kondoyo-kondo
提出日時 2018-03-06 22:24:35
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 433 ms / 5,000 ms
コード長 1,590 bytes
コンパイル時間 12,927 ms
コンパイル使用メモリ 432,436 KB
実行使用メモリ 62,904 KB
最終ジャッジ日時 2024-04-30 17:02:27
合計ジャッジ時間 25,177 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 315 ms
60,240 KB
testcase_01 AC 313 ms
60,372 KB
testcase_02 AC 303 ms
56,840 KB
testcase_03 AC 419 ms
60,512 KB
testcase_04 AC 377 ms
60,640 KB
testcase_05 AC 428 ms
62,756 KB
testcase_06 AC 433 ms
60,488 KB
testcase_07 AC 379 ms
60,528 KB
testcase_08 AC 407 ms
60,588 KB
testcase_09 AC 358 ms
60,580 KB
testcase_10 AC 421 ms
60,716 KB
testcase_11 AC 373 ms
60,448 KB
testcase_12 AC 414 ms
60,464 KB
testcase_13 AC 418 ms
60,572 KB
testcase_14 AC 326 ms
60,376 KB
testcase_15 AC 330 ms
60,428 KB
testcase_16 AC 423 ms
62,660 KB
testcase_17 AC 427 ms
62,768 KB
testcase_18 AC 426 ms
62,904 KB
testcase_19 AC 428 ms
62,716 KB
testcase_20 AC 318 ms
60,224 KB
testcase_21 AC 320 ms
60,308 KB
testcase_22 AC 325 ms
60,212 KB
testcase_23 AC 321 ms
60,416 KB
testcase_24 AC 330 ms
60,464 KB
testcase_25 AC 332 ms
60,440 KB
testcase_26 AC 323 ms
60,260 KB
testcase_27 AC 318 ms
60,300 KB
testcase_28 AC 308 ms
56,948 KB
testcase_29 AC 383 ms
60,624 KB
testcase_30 AC 362 ms
60,504 KB
testcase_31 AC 306 ms
56,940 KB
testcase_32 AC 307 ms
56,920 KB
testcase_33 AC 303 ms
56,920 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:5:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:21:30: warning: parameter 'blockNum' is never used
fun block(boxWidth: String?, blockNum: String?, blockWidth: String?): String {
                             ^
Main.kt:25:9: warning: name shadowed: boxWidth
    val boxWidth = boxWidth?.toInt() ?: 0
        ^
Main.kt:29:9: warning: name shadowed: blockWidth
    val blockWidth = blockWidth
        ^

ソースコード

diff #

/**
 * エントリポイント
 * @param args コマンドライン引数
 */
fun main(args: Array<String>) {

    // readLine で標準入力から文字列を取得します。
    val l = readLine()
    val n = readLine()
    val w = readLine()

    println(block(l, n, w))
}

/**
 * 箱に入るブロックの数を計算して返します。
 * @param boxWidth 箱の幅
 * @param blockNum ブロックの数
 * @param blockWidth 各ブロックの幅
 */
fun block(boxWidth: String?, blockNum: String?, blockWidth: String?): String {

    // ?. でnullでない場合のみ後ろのメソッドを実行します。?. でメソッドを呼んだ場合は nullable が返ります。
    // ?: (エルビス演算子)でnullの場合に右の値を返します。これで nullable から not null になります。
    val boxWidth = boxWidth?.toInt() ?: 0

    // map で List<String> を List<Int> に変換します。
    // emptyList で空のListを返します。
    val blockWidth = blockWidth
            ?.split(" ")
            ?.map { it.toInt() }
            ?: emptyList()

    var inBlockNum = 0
    var inBlockWidth = 0

    // sorted で List をソートします。 sortedDescending で降順にソートします。
    val sortBlockWidth = blockWidth.sorted()

    // 小さいブロックから数えて、箱の幅を超えたら終了
    for (i in sortBlockWidth) {
        if (i + inBlockWidth <= boxWidth) {
            inBlockNum++
            inBlockWidth += i
        } else {
            break
        }
    }

    return inBlockNum.toString()
}
0