Baekjoon algorithm training

[백준] 2884 (알람 시계)

interfacer_han 2023. 11. 21. 16:39

#1 알고리즘

H의 최댓값이 23이고 M의 최댓값이 59라는 이유로, totalMinute < 0일 때, totalMinute += (23 * 60) + 59라고 적으면 안 된다. 23과 59은 표기상의 최댓값일 뿐, 실제 하루는 1440분이기 때문이다.

 

#2 코드

#2-1 자바

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        String[] testCase = sc.nextLine().trim().split(" ");
        int hour = Integer.parseInt(testCase[0]);
        int minute = Integer.parseInt(testCase[1]);
        
        sc.close();
        
        int totalMinute = hour * 60 + minute;
        
        totalMinute -= 45;
        if(totalMinute < 0) {
            totalMinute += 24 * 60; // = 1440
        }
        
        int changedHour = totalMinute / 60;
        int changedMinute = totalMinute - (changedHour * 60);
        
        System.out.println(Integer.toString(changedHour) + " " + Integer.toString(changedMinute));
    }
}

 

#2-2 코틀린

fun main() {
    val testCase = readln()!!.trim().split(" ")
    val hour = testCase[0].toInt()
    val minute = testCase[1].toInt()
    
    var totalMinute = hour * 60 + minute
    
    totalMinute -= 45
    if(totalMinute < 0) {
        totalMinute += (23 * 60) + 60 // = 1440
    }
    
    val changedHour = totalMinute / 60
    val changedMinute = totalMinute - (changedHour * 60)
    
    println("${changedHour} ${changedMinute}")
}

'Baekjoon algorithm training' 카테고리의 다른 글

[백준] 10250 (ACM 호텔)  (0) 2023.11.23
[백준] 2920 (음계)  (0) 2023.11.22
[백준] 11650 (좌표 정렬하기)  (0) 2023.11.18
[백준] 10989 (수 정렬하기 3)  (0) 2023.11.17
[백준] 10809 (알파벳 찾기)  (0) 2023.11.16