結果

問題 No.884 Eat and Add
ユーザー hirogahiroga
提出日時 2019-12-30 09:15:09
言語 Kotlin
(1.9.23)
結果
RE  
実行時間 -
コード長 1,224 bytes
コンパイル時間 13,163 ms
コンパイル使用メモリ 421,356 KB
実行使用メモリ 57,796 KB
最終ジャッジ日時 2023-08-07 14:01:31
合計ジャッジ時間 20,217 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
55,368 KB
testcase_01 AC 301 ms
55,516 KB
testcase_02 AC 300 ms
55,612 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 304 ms
55,416 KB
testcase_11 AC 304 ms
55,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://yukicoder.me/problems/no/884

package yukicoder

import kotlin.math.min
import kotlin.math.pow

fun main() {
    val n = readLine()!!
    println(no884(n))
}

fun no884(n: String): Int {
    val num = Integer.parseInt(n, 2)
    return get2の累乗との差が0になるまで2の累乗を足すか引くかし続けた回数(num)
}

fun get2の累乗との差が0になるまで2の累乗を足すか引くかし続けた回数(num: Int, count: Int = 0): Int {
    val 最寄りの2の累乗との差 = get最寄りの2の累乗との差(num)
    return if (最寄りの2の累乗との差 == 0) {
        count + 1
    } else {
        get2の累乗との差が0になるまで2の累乗を足すか引くかし続けた回数(最寄りの2の累乗との差, count + 1)
    }
}

val `2の累乗リスト` = (0..200000).map { 2.0.pow(it.toDouble()).toInt() }
fun get最寄りの2の累乗との差(num: Int): Int {
    for (`2の累乗` in `2の累乗リスト`) {
        if (num == `2の累乗`) {
            return 0
        } else if (num < `2の累乗`) {
            val diff = `2の累乗` - num
            return min(diff, `2の累乗` / 2 - diff)
        }
    }
    throw Exception("Impossible")
}

0