Problem Solving

[백준/Java] 2525번 - 왜 자꾸 틀리지???

Jintiago 2022. 3. 24. 05:07

현재 시간과 원하는 시간 간격을 입력하면 해당 시간이 경과한 시간을 출력해야 한다.

10 10

20

입력하면

10 30

이렇게 출력되어야 한다.

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int h = sc.nextInt();
        int m = sc.nextInt();
        int t = sc.nextInt();
        m += t;
        
        if ( m >= 60 ) {
            h+=1;
            if ( h == 24 ) {
                h = 0;
            }
            m -= 60;
        }
        
        System.out.println(h + " " + m);
    }
}

근데 자꾸 틀린다. 왜지?? 

 

고민하다 보니, 60분을 초과하는 시간을 입력하는 경우를 고려하지 않았다는 걸 깨달았다.

import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        
        int h = sc.nextInt();
        int m = sc.nextInt();
        int t = sc.nextInt();
        m += t;
        int newM = m % 60; // 표시될 분
        int newH = h + (m/60); // 표시될 시
        
        if (newH >= 24) { 
            newH %= 24; // 24시가 넘어가는 경우 시간 설정
        }
        
        System.out.println(newH + " " + newM);
        
    }
}

이 간단한 걸 20분이나 걸려서 풀었다..ㅋㅋㅋ 밤이 늦어서 그런가 점점 머리가 멍해진다.

잠이 안 오지만 이제 자러 가야겠다ㅠ