문자열로 된 시간의 차이를
구하는 문제이다
구해야하는 값을 가장 작은 단위로 환산하면
가볍게 풀 수 있는 문제이다
다만, 출력 조건을 자세하게 읽어보지 않으면
삽질을 할 수 있으니 주의해야한다 ^^
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String todayTime = sc.nextLine();
String goalTime = sc.nextLine();
String time = superTimer(todayTime.split(":"), goalTime.split(":"));
System.out.print(time);
}
private static String superTimer(String[] today, String[] goal){
int todayTime = Integer.parseInt(today[0]) * 3600 + Integer.parseInt(today[1]) * 60 + Integer.parseInt(today[2]);
int goalTime = Integer.parseInt(goal[0]) * 3600 + Integer.parseInt(goal[1]) * 60 + Integer.parseInt(goal[2]);
int needTime = goalTime - todayTime;
if(needTime<=0) needTime += 24 * 3600;
int hour = needTime / 3600;
int minute = (needTime % 3600) / 60;
int second = needTime % 60;
String ans = String.format("%02d:%02d:%02d", hour, minute, second);
return ans;
}
'코딩테스트' 카테고리의 다른 글
[구름] 수열 (python) (0) | 2024.11.17 |
---|---|
[구름] 체크 카드 (python) (0) | 2024.11.10 |
[구름] 발전기 (python) (0) | 2024.10.27 |
백준 1543 - 문서 검색 (1) | 2024.10.20 |
[구름] 딱지놀이 (0) | 2024.10.17 |