[백준] Accumulator Battery (16648)(kotlin)

원본 문제

문제 설명

Anna loves her cell phone and becomes very sad when its battery level drops to 0 percent.

In normal mode, Anna’s phone battery drains at a constant speed. When the battery level reaches 20 percent, the phone automatically switches to eco mode. In eco mode, the battery drains two times slower than in normal mode.

Alex has invited Anna for a date. Anna needs t minutes to get from her home to the meeting place. When Anna leaves home, her phone’s battery level is 100 percent. At the moment she reaches the meeting place, the battery level will be p percent.

Alex wonders for how long Anna will be in a good mood after they meet. Help him solve this problem!

입력

The only line of the input contains two integers t and p — time Anna needs to get from her home to the meeting place, in minutes, and the battery level of her phone at the moment of meeting, in percent (1 ≤ t ≤ 360; 1 ≤ p ≤ 99).

출력

Output a single real number — time since the moment of meeting before Anna’s phone runs out of battery, in minutes.

Your answer will be considered correct if its absolute or relative error doesn’t exceed 10^−4.

테스트 케이스

입력 출력
30 70 90.0
120 5 10.909091

문제 풀이1

import java.util.Scanner

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val t = sc.nextInt()
    val p = sc.nextInt()

    if (p >= 20) {
        val nm = 1.0 * (100 - p) / t
        println((p - 20) / nm + (40 / nm))
    } else {
        val nm = 1.0 * (80 + (20 - p) * 2) / t
        println(p * 2 / nm)
    }
}

문제 풀이2

솔루션

import java.util.Scanner

fun main(args: Array<String>) = with(Scanner(System.`in`)) {
    val t = nextInt()
    val p = nextInt()
    val velocity = when {
        p >= 20 -> 1.0 * (100 - p) / t
        else -> 1.0 * (80 + (20 - p) * 2) / t
    }
    val timeLeft = Math.min(p, 20) / (velocity * 0.5) + Math.max(0, p - 20) / velocity
    println(timeLeft)
}