ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 - 13223 소금폭탄
    Tech-blog/Algorithm 2023. 8. 4. 15:27

    문제는 소금을 투하할 시간 - 현재시간을 하면 되는 간단한 문제였다.

    package baekjun.String;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Baekjoon13223 {
        public static String time(int time){ // 00:00:01 << 이런 형식의 숫자를 출력하기 위한 메서드
            String returnTime = new String();
            if(time < 10){
                return returnTime = "0" + time;
            }else {
                return returnTime = String.valueOf(time);
            }
        }
        public static int[] toInteger(String time){ // 시간을 계산하기위한 toInteger 메서드
            int[] times = new int[3];
    
            int hour = Integer.parseInt(time.substring(0, 2));
            times[0] = hour;
            int minute = Integer.parseInt(time.substring(3, 5));
            times[1] = minute;
            int sec = Integer.parseInt(time.substring(6, 8));
            times[2] = sec;
            return times;
        }
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String nowTime = br.readLine();
            String actionTime = br.readLine();
    
            int[] nowTimes = toInteger(nowTime);
            int[] actionTimes = toInteger(actionTime);
    
            int answerHour = 0;
            int answerMinute = 0;
            int answerSec = 0;
    
            if (nowTimes[2] > actionTimes[2]){
                actionTimes[2] += 60;
                actionTimes[1] -= 1;
            }
            answerSec = actionTimes[2] - nowTimes[2];
            if (nowTimes[1] > actionTimes[1]){
                actionTimes[1] += 60;
                actionTimes[0] -= 1;
            }
            answerMinute = actionTimes[1] - nowTimes[1];
    
            if (nowTimes[0] > actionTimes[0]){
                actionTimes[0] += 24;
            }
            answerHour = actionTimes[0] - nowTimes[0];
            if (answerHour == 0 && answerMinute == 0 && answerSec == 0){
                answerHour = 24;
            }
            String hour = time(answerHour);
            String minute = time(answerMinute);
            String sec = time(answerSec);
    
    
            System.out.println(hour + ":" + minute + ":" + sec);
        }
    }

    문제 풀이 코드는 이렇게 생각보다 길게 나왔는데 아무리봐도 틀린 정답이 아닌데 틀렸다고 나오길래 문제를 다시 결과..

    소금 투하시간과 현재시간이 같다면 00:00:00이 아닌 24:00:00 형태로 출력을 해야 했기 때문에 위코드에서 24번째 if문을 추가해서 맞추게 되었다.

    댓글

Designed by Tistory.