알고리즘

[BAEKJOON 백준 / Java] 소금 폭탄 13223

작은달팽이 2023. 6. 3. 16:11

* Java 8 기준

[백준/Java] 소금 폭탄 13223

문제

13223번: 소금 폭탄

 

13223번: 소금 폭탄

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

www.acmicpc.net

문제: HH:MM:SS 포맷의 두 시각의 차이를 HH:MM:SS 포맷으로 출력하기

풀이

  1. ':' 문자를 기준으로 시간, 분, 초를 쪼갠다.
  2. 두 시간, 분, 초의 차이를 계산한다.
  3. 구해진 시간을 HH:MM:SS 형태로 출력한다.
    1. 각 값이 한 자릿수인 경우 앞에 ‘0’을 붙여 출력해야 한다.
  4. 주의사항 : 로봇팔이 소금을 투하할때까지 필요한 시간을 hh:mm:ss로 출력한다. 이 시간은 1초보다 크거나 같고, 24시간보다 작거나 같다.
    1. 필요시간이 음수로 나오는 경우
    2. 필요시간이 정확히 하루가 차이나는 경우
      1. 필요시간은 00:00:00 이 아닌 24:00:00 으로 출력되야 한다.
    → 결국 두 조건 모두 결과값에 24시간을 더해줘야 한다.

🌟 Tip : 계층적으로 표현되는 각 단위를 계산할 때, 가장 작은 단위로 통일하면 더 편하게 계산할 수 있다. 이 문제에서는 ‘초’가 가장 작은 단위이므로 초로 통일해서 풀이.


코드

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] current = sc.next().split(":");
        String[] drop = sc.next().split(":");

        int currentHour = Integer.parseInt(current[0]);
        int currentMinute = Integer.parseInt(current[1]);
        int currentSecond = Integer.parseInt(current[2]);
        int currentSecondAmount = currentHour * 3600 + currentMinute * 60 + currentSecond;

        int dropHour = Integer.parseInt(drop[0]);
        int dropMinute = Integer.parseInt(drop[1]);
        int dropSecond = Integer.parseInt(drop[2]);
        int dropSecondAmount = dropHour * 3600 + dropMinute * 60 + dropSecond;

        int needSecondAmount = dropSecondAmount - currentSecondAmount;
        if (needSecondAmount <= 0)
            needSecondAmount += 24 * 3600;
        int needHour = needSecondAmount / 3600;
        int needMinute = (needSecondAmount % 3600) / 60;
        int needSecond = needSecondAmount % 60;

        System.out.printf("%02d:%02d:%02d",needHour,needMinute,needSecond);
    }
}

// String[] split(String regex)
// return split(regex, 0);
// 문자열을 지정된 분리자(regex)로 나누어 문자열 배열에 담아 반환한다.
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String current = sc.next();
        String drop = sc.next();

        int currentHour = Integer.parseInt(current.substring(0, 2));
        int currentMinute = Integer.parseInt(current.substring(3, 5));
        int currentSecond = Integer.parseInt(current.substring(6));
        int currentSecondAmount = currentHour * 3600 + currentMinute * 60 + currentSecond;

        int dropHour = Integer.parseInt(drop.substring(0, 2));
        int dropMinute = Integer.parseInt(drop.substring(3, 5));
        int dropSecond = Integer.parseInt(drop.substring(6));
        int dropSecondAmount = dropHour * 3600 + dropMinute * 60 + dropSecond;
        
        int needSecondAmount = dropSecondAmount - currentSecondAmount;
        if (needSecondAmount <= 0)
            needSecondAmount += 24 * 3600;
        int needHour = needSecondAmount / 3600;
        int needMinute = (needSecondAmount % 3600) / 60;
        int needSecond = needSecondAmount % 60;

        System.out.printf("%02d:%02d:%02d",needHour,needMinute,needSecond);
    }
}

// String substring(int beginIndex, int endIndex)
// return StringUTF16.newString(value, beginIndex, endIndex - beginIndex);
// 주어진 시작위치(beginIndex)부터 끝 위치(endIndex) 범위에 포함된 문자열을 얻는다.
// 이 때, 끝 위치의 문자는 포함되지 않는다.
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String current = sc.next();
        String drop = sc.next();

        int currentHour = (current.charAt(0) + '0') * 10 + current.charAt(1) + '0';
        int currentMinute = (current.charAt(3) + '0') * 10 + current.charAt(4) + '0';
        int currentSecond = (current.charAt(6) + '0') * 10 + current.charAt(7) + '0';
        int currentSecondAmount = currentHour * 3600 + currentMinute * 60 + currentSecond;

        int dropHour = (drop.charAt(0) + '0') * 10 + drop.charAt(1) + '0';
        int dropMinute = (drop.charAt(3) + '0') * 10 + drop.charAt(4) + '0';
        int dropSecond = (drop.charAt(6) + '0') * 10 + drop.charAt(7) + '0';
        int dropSecondAmount = dropHour * 3600 + dropMinute * 60 + dropSecond;

        int needSecondAmount = dropSecondAmount - currentSecondAmount;
        if (needSecondAmount <= 0)
            needSecondAmount += 24 * 3600;
        int needHour = needSecondAmount / 3600;
        int needMinute = (needSecondAmount % 3600) / 60;
        int needSecond = needSecondAmount % 60;

        System.out.printf("%02d:%02d:%02d", needHour, needMinute, needSecond);
    }
}