𝙰𝚕𝚐𝚘𝚛𝚒𝚝𝚑𝚖/𝙱𝚊𝚎𝚔𝚓𝚘𝚘𝚗
[백준/Java] 경고 3029
해버니
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);
}
}
정답
반응형