본문 바로가기

Algorithm/CodeUp

[CodeUp_Java] Q1174 : 30분 전 (if는 아직...)

반응형

안녕하세요! Plitche(플리체)입니다.
이번 포스팅의 주제는 Q1174 : 30분 전 (if는 아직...) (자바, JAVA)입니다.

intro

Question

문제 설명

입력

시와 분이 입력된다.
(시의 범위 : 0~ 23)
(분의 범위 : 0~ 59)

출력

입력된 시간의 30분 전의 시간을 출력하시오.

예시

  • 입력 : 12 35
  • 출력 : 12 5

Solution (풀이)

  • 풀이 1 : 메모리 11244, 시간 91
public class Answer1 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        // 시와 분을 입력 받는다.
        int hour = Integer.parseInt(st.nextToken());
        int minutes = Integer.parseInt(st.nextToken());

        // 시간에 24시(1일)을 더해서 분으로 환산한다.
        int total = (hour+24)*60+minutes;
        // 30분을 뺸다.
        total -= 30;

        hour = (total/60)%24;    // 60으로 나눈 몫의 24로 나눈 나머지 (1일이 넘을수 있기 때문에)
        minutes = total%60;        // 60으로 나눈 나머지

        //출력
        bw.append(String.valueOf(hour + " ")).append(String.valueOf(minutes));
        bw.flush();
        br.close();
        bw.close();
    }
}
  • 풀이 2 : 메모리 11216, 시간 70
public class Answer2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        StringBuilder sb = new StringBuilder();

        // 시와 분을 입력 받는다.
        int hour = Integer.parseInt(st.nextToken());
        int minutes = Integer.parseInt(st.nextToken());

        // 시간에 24시(1일)을 더해서 분으로 환산한다.
        int total = (hour+24)*60+minutes;
        // 30분을 뺸다.
        total -= 30;

        hour = (total/60)%24;    // 60으로 나눈 몫의 24로 나눈 나머지 (1일이 넘을수 있기 때문에)
        minutes = total%60;        // 60으로 나눈 나머지

        //출력
        sb.append(hour + " ").append(minutes);
        System.out.print(sb);
    }
}

Ranking(순위)

반응형