13223번: 소금 폭탄
첫째 줄에는 현재 시각이 hh:mm:ss로 주어진다. 시간의 경우 0≤h≤23 이며, 분과 초는 각각 0≤m≤59, 0≤s≤59 이다. 두 번째 줄에는 소금 투하의 시간이 hh:mm:ss로 주어진다.
www.acmicpc.net
풀이
':' 문자를 기준으로 시간, 분, 초를 쪼갠다
- 방법 1 : 각 단위의 인덱스에서 10의 자리와 1의 자리를 구해 계산 가능
String time = "09:10:59";
int hour = (time.charAt(0) - '0') * 10 + time.charAt(1) - '0';
int minute = (time.charAt(3) - '0') * 10 + time.charAt(4) - '0';
int second = (time.charAt(6) - '0') * 10 + time.charAt(7) - '0';
- 방법 2 : String.substring을 사용하면 원하는 문자열만 떼올 수 있다
String time = "09:10:59";
int hour = Integer.parseInt(time.substring(0, 2));
int minute = Integer.parseInt(time.substring(3, 5));
int second = Integer.parseInt(time.substring(6, 8));
- 방법 3 : String.split을 사용하면 토큰을 기준으로 문자열을 나눠준다
String[] time = "09:10:59".split(":");
int hour = Integer.parseInt(time[0]);
int minute = Integer.parseInt(time[1]);
int second = Integer.parseInt(time[2]);
답안
import java.util.Scanner;
class Main
{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String[] current = sc.next().split(":");
String[] target = sc.next().split(":");
int currentHour = Integer.parseInt(current[0]);
int currentMinute = Integer.parseInt(current[1]);
int currentSecond = Integer.parseInt(current[2]);
int targetHour = Integer.parseInt(target[0]);
int targetMinute = Integer.parseInt(target[1]);
int targetSecond = Integer.parseInt(target[2]);
int currentSecondAmount = currentHour * 3600 + currentMinute * 60 + currentSecond;
int targetSecondAmount = targetHour * 3600 + targetMinute * 60 + targetSecond;
int NeedSecondAmount = targetSecondAmount - currentSecondAmount;
if (NeedSecondAmount <= 0) NeedSecondAmount += 24 * 3600; // 출력에서 시간은 1초보다 크거나 작아야 함
int needHour = NeedSecondAmount / 3600;
int needMinute = (NeedSecondAmount % 3600) / 60;
int needSecond = NeedSecondAmount % 60;
System.out.println(String.format("%02d:%02d:%02d", needHour, needMinute, needSecond));
}
}
'백준 문제풀이 > 못 푼 문제' 카테고리의 다른 글
[백준 1157] 단어 공부 (0) | 2024.02.18 |
---|---|
[백준 1919] 애너그램 만들기 (0) | 2024.02.17 |
13223번: 소금 폭탄
첫째 줄에는 현재 시각이 hh:mm:ss로 주어진다. 시간의 경우 0≤h≤23 이며, 분과 초는 각각 0≤m≤59, 0≤s≤59 이다. 두 번째 줄에는 소금 투하의 시간이 hh:mm:ss로 주어진다.
www.acmicpc.net
풀이
':' 문자를 기준으로 시간, 분, 초를 쪼갠다
- 방법 1 : 각 단위의 인덱스에서 10의 자리와 1의 자리를 구해 계산 가능
String time = "09:10:59"; int hour = (time.charAt(0) - '0') * 10 + time.charAt(1) - '0'; int minute = (time.charAt(3) - '0') * 10 + time.charAt(4) - '0'; int second = (time.charAt(6) - '0') * 10 + time.charAt(7) - '0';
- 방법 2 : String.substring을 사용하면 원하는 문자열만 떼올 수 있다
String time = "09:10:59"; int hour = Integer.parseInt(time.substring(0, 2)); int minute = Integer.parseInt(time.substring(3, 5)); int second = Integer.parseInt(time.substring(6, 8));
- 방법 3 : String.split을 사용하면 토큰을 기준으로 문자열을 나눠준다
String[] time = "09:10:59".split(":"); int hour = Integer.parseInt(time[0]); int minute = Integer.parseInt(time[1]); int second = Integer.parseInt(time[2]);
답안
import java.util.Scanner; class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); String[] current = sc.next().split(":"); String[] target = sc.next().split(":"); int currentHour = Integer.parseInt(current[0]); int currentMinute = Integer.parseInt(current[1]); int currentSecond = Integer.parseInt(current[2]); int targetHour = Integer.parseInt(target[0]); int targetMinute = Integer.parseInt(target[1]); int targetSecond = Integer.parseInt(target[2]); int currentSecondAmount = currentHour * 3600 + currentMinute * 60 + currentSecond; int targetSecondAmount = targetHour * 3600 + targetMinute * 60 + targetSecond; int NeedSecondAmount = targetSecondAmount - currentSecondAmount; if (NeedSecondAmount <= 0) NeedSecondAmount += 24 * 3600; // 출력에서 시간은 1초보다 크거나 작아야 함 int needHour = NeedSecondAmount / 3600; int needMinute = (NeedSecondAmount % 3600) / 60; int needSecond = NeedSecondAmount % 60; System.out.println(String.format("%02d:%02d:%02d", needHour, needMinute, needSecond)); } }
'백준 문제풀이 > 못 푼 문제' 카테고리의 다른 글
[백준 1157] 단어 공부 (0) | 2024.02.18 |
---|---|
[백준 1919] 애너그램 만들기 (0) | 2024.02.17 |