반응형

백준 2884번 알람 시계 풀이 코드

C | C++ | Java | Python


풀이

시간(h)과 분(m)을 입력받고 분이 45분보다 크다면 단순히 45분을 빼고 출력, 그렇지 않다면 15분을 더하고 1시간을 뺀 후 출력합니다(60분 체계에서 45분을 뒤로 감는 것은 해당 분에서 15분을 더하는 것과 동일합니다). 시간이 0시였다면 23으로 바꾸는 예외처리를 잊지 않도록 주의합니다.

코드

#include <stdio.h>

int main(){
    int h, m;
    scanf("%d %d", &h, &m);
    
    if(m >= 45){
        m -= 45;
        printf("%d %d", h, m);
    }
    else{
        m += 15;
        if(h == 0)
            h = 23;
        else
            h -= 1;
        printf("%d %d", h, m);
    }
    return 0;
}
#include <iostream>

int main(){
    int h, m;
    std::cin>>h>>m;
    if(m >= 45){
        m -= 45;
        std::cout<<h<<' '<<m;
    }
    else{
        m += 15;
        if(h == 0)
            h = 23;
        else
            h -= 1;
        std::cout<<h<<' '<<m;
    }
    return 0;
}
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int m = sc.nextInt();
        if(m >= 45){
            m -= 45;
            System.out.println(h + " " + m);
        }
        else{
            m += 15;
            if(h == 0)
                h = 23;
            else
                h -= 1;
            System.out.println(h + " " + m);
        }
        return;
    }
}
h,m=  map(int,input().split())
if m >= 45:
    print(h, m-45)
else:
    m += 15
    if h == 0:
        h = 23
    else:
        h -= 1
    print(h, m)

문제 출처

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

반응형

+ Recent posts