코딩테스트
[코딩테스트] 백준 6번 알람 시계 (백준 2884번) - Java
원코딩
2022. 10. 12. 15:13
2884번: 알람 시계
상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해보았지만,
www.acmicpc.net
가장 어려웠던 부분은 24시간을 계산해야되는 점이었다.
(23시에서 0시로 넘어가야 하는 부분)
너무 잘 설명해주신 분 덕분에 고비를 잘 넘겼다. (감사합니다) [백준] 2884번 : 알람 시계 - JAVA [자바] (tistory.com)
▶ 풀이
핵심은 45분 보다 적은지 큰지 여부를 확인하는 것이다.
45분 보다 적다면 시간(h)을 뺀 후 나머지를 분(m)에서 빼고
45분 보다 크다면 분(m)에서만 빼도 된다.
1. Scanner
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int H=scan.nextInt();
int M=scan.nextInt();
scan.close();
if(M<45)
{
H=H-1;
M= 60 - (45 - M); // 45분을 뺀 나머지 분은 60분에서 뺀다.
if(H<0) { // 0시에서 -1을 해버리면 '-1시'가 되기 때문에 23시로 변경.
H=23;
}
System.out.println(H+" "+M);
}
else
{
M=M-45;
System.out.println(H+" "+M);
}
}
}
2. BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str=br.readLine().split(" ");
int H=Integer.parseInt(str[0]);
int M=Integer.parseInt(str[1]);
if(M<45)
{
H=H-1;
M= 60 - (45 - M); // 45분을 뺀 나머지 분은 60분에서 뺀다.
if(H<0) { // 0시에서 -1을 해버리면 '-1시'가 되기 때문에 23시로 변경.
H=23;
}
System.out.println(H+" "+M);
}
else
{
M=M-45;
System.out.println(H+" "+M);
}
}
}