본문 바로가기
BaekJoon

[BaekJoon][Kotlin] 13223번 - 소금 폭탄

by jinwo_o 2024. 12. 4.

https://www.acmicpc.net/problem/13223

문제

철수는 화학 시험을 망치고, 애꿎은 화학 선생님에게 복수를하기로 한다.

철수는 집에서 만든 자동 로봇팔을 선생님의 책상에 숨겨, 선생님이 수업을 시작하려 들어온 순간 숨겨놓은 로봇팔을 이용해 선생님을 혼내주려고한다. 철수는 선생님이 늘 애용하는 물컵에 시간이 되면 로봇팔이 소금을 잔뜩 집어넣도록 프로그램을 짜려고한다.

철수는 현재시각과 선생님이 언제 컵을 사용할지 시간을 알고있지만, 수 계산에 정말 약해서 로봇팔에 입력해야할 시간 계산을 못한다. 철수가 로봇팔에 알맞은 시간을 입력할수 있도록 도와주자.

입력

첫째 줄에는 현재 시각이 hh:mm:ss로 주어진다. 시간의 경우 0≤h≤23 이며, 분과 초는 각각 0≤m≤59, 0≤s≤59 이다.

두 번째 줄에는 소금 투하의 시간이 hh:mm:ss로 주어진다.

출력

로봇팔이 소금을 투하할때까지 필요한 시간을 hh:mm:ss로 출력한다. 이 시간은 1초보다 크거나 같고, 24시간보다 작거나 같다.

예제 입력 1 복사

20:00:00
04:00:00

예제 출력 1 복사

08:00:00

예제 입력 2 복사

12:34:56
14:36:22

예제 출력 2 복사

02:01:26

코드 1

fun main() {
    val input1 = readln().split(":")
    val currentHour = input1[0].toInt()
    val currentMinute = input1[1].toInt()
    val currentSecond = input1[2].toInt()

    val input2 = readln().split(":")
    val targetHour = input2[0].toInt()
    val targetMinute = input2[1].toInt()
    val targetSecond = input2[2].toInt()

    var hour = targetHour - currentHour
    var minute = targetMinute - currentMinute
    var second = targetSecond - currentSecond

    if (second < 0) {
        second += 60
        minute--
    }

    if (minute < 0) {
        minute += 60
        hour--
    }

    if (hour <= 0) {
        hour += 24
    }

    print(String.format("%02d:%02d:%02d", hour, minute, second))
}

 

코드 2

fun main() {
    val input1 = readln().split(":")
    val currentHour = input1[0].toInt()
    val currentMinute = input1[1].toInt()
    val currentSecond = input1[2].toInt()
    val currentSecondAmount = currentHour * 3600 + currentMinute * 60 + currentSecond

    val input2 = readln().split(":")
    val targetHour = input2[0].toInt()
    val targetMinute = input2[1].toInt()
    val targetSecond = input2[2].toInt()
    val targetSecondAmount = targetHour * 3600 + targetMinute * 60 + targetSecond

    var needSecondAmount = targetSecondAmount - currentSecondAmount
    if (needSecondAmount <= 0) {
        needSecondAmount += 24 * 3600
    }

    val hour = needSecondAmount / 3600
    val minute = (needSecondAmount % 3600) / 60
    val second = needSecondAmount % 60

    print(String.format("%02d:%02d:%02d", hour, minute, second))
}