ํ•ด๋ฒ„๋‹ˆ 2023. 11. 12. 16:31
๋ฐ˜์‘ํ˜•

 

 

๋ฌธ์ œ

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

 

3029๋ฒˆ: ๊ฒฝ๊ณ 

์ฒซ์งธ ์ค„์— ํ˜„์žฌ ์‹œ๊ฐ„์ด hh:mm:ss ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. (์‹œ, ๋ถ„, ์ดˆ) hh๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 23๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, ๋ถ„๊ณผ ์ดˆ๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 59๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋‚˜ํŠธ๋ฅจ์„ ๋˜์งˆ ์‹œ๊ฐ„

www.acmicpc.net

 

 

 

 

 

 

 

 

 

 

 

 

 

ํ’€์ด

 

๋ญ”๊ฐ€ ๋ง์žฅ๋‚œ ํ•˜๋Š” ๋ฌธ์ œ๊ฐ™๋‹ค... -.- 

"์ ์–ด๋„ 1์ดˆ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค"๊ฐ€ ํฌ์ธํŠธ์˜€๋˜ ๋ฌธ์ œ์ด๋‹ค. 

 

package algorithm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public String calculateTimeDifference(String currentTime, String bomb) {
        int ctHours = Integer.parseInt(currentTime.substring(0, 2));
        int ctMinutes = Integer.parseInt(currentTime.substring(3, 5));
        int ctSeconds = Integer.parseInt(currentTime.substring(6, 8));

        int bHours = Integer.parseInt(bomb.substring(0, 2));
        int bMinutes = Integer.parseInt(bomb.substring(3, 5));
        int bSeconds = Integer.parseInt(bomb.substring(6, 8));

        int secondsDiff = (bSeconds - ctSeconds + 60) % 60;
        if (bSeconds - ctSeconds < 0) {
            bMinutes--;
        }
        int minutesDiff = (bMinutes - ctMinutes + 60) % 60;
        if (bMinutes - ctMinutes < 0) {
            bHours--;
        }
        int hoursDiff = (bHours - ctHours + 24) % 24;

        String timeFormat = String.format("%02d:%02d:%02d", hoursDiff, minutesDiff, secondsDiff);
        
        if (hoursDiff == 0 && minutesDiff == 0 && secondsDiff == 0) {
            timeFormat = "24:00:00";
        }
        

        return timeFormat;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String currentTime = br.readLine();
        String bomb = br.readLine();

        Main m = new Main();
        String answer = m.calculateTimeDifference(currentTime, bomb);
        System.out.println(answer);
    }
}

 

 

 

 

 

 

 

 

 

์ •๋‹ต 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•