Notice
Recent Posts
Recent Comments
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

백준[Java] 2884번 알람 시계 본문

IT/Java[백준]

백준[Java] 2884번 알람 시계

빌드이너프 2023. 2. 14. 17:12

https://www.acmicpc.net/problem/2884

 

2884번: 알람 시계

상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해보았지만,

www.acmicpc.net


문제풀이

import java.util.Scanner;
public class _2884_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int H = sc.nextInt();
        int M = sc.nextInt();

        sc.close();

        M -= 45;

        if (M < 0){
             //H -= 1;
            H --;
            M+=60;

        if (H < 0)
            H += 24;
        }
        System.out.printf("%d %d", H, M);

    }
}
  • 입력된 시간과 분에서 45분 전의 시간을 구하는 문제
  • 시(hour)와 분(minute) 변수 정해줌

 

  • minute에서 45분을 먼저 빼준다
  • 만약 minute가 음수로 바뀌면 1시간 = 60분이기때문에 60을 더해준다
  • hour가 0시였는데 -1을 해주어 -1이 될 경우 하루는 24시간 이기 때문에 24-1=23
  • 즉, 23을 hour에 선언해준다

더보기

if문을 2번 사용했는데 더 좋은 방법이 무조건 있을거 같고 찾는다면 추가해 둘 예정


문제풀이 2

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class _2884_2 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String str = br.readLine();
        StringTokenizer st = new StringTokenizer(str, " ");
        int H = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        M -= 45;

        if (M < 0){
            H --;
            M+=60;
        if (H < 0)
            H += 24;
        }
        System.out.printf("%d %d", H, M);

    }
}

문제풀이 3

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class _2884_3 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        int H = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        M -= 45;

        if (M < 0){
            H --;
            M+=60;
        if (H < 0)
            H += 24;
        }
        System.out.printf("%d %d", H, M);

    }
}

문제풀이 4

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class _2884_4 {
    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]);

        M -= 45;

        if (M < 0){
            H --;
            M+=60;
        if (H < 0)
            H += 24;
        }
        System.out.printf("%d %d", H, M);

    }
}

 

'IT > Java[백준]' 카테고리의 다른 글

백준[Java] 2480번 주사위 세개  (0) 2023.02.15
백준[Java] 2525번 오븐 시계  (2) 2023.02.14
백준[Java] 14681번 사분면 고르기  (0) 2023.02.14
백준[Java] 2753번 윤년  (0) 2023.02.14
백준[Java] 9498번 시험 성적  (0) 2023.02.14