Post

[BaekJoon] 2530번 - 인공지능 시계 [Java][C++]

[BaekJoon] 2530번 - 인공지능 시계 [Java][C++]

문제 링크


1. 문제 풀이


$A$ 시 $B$ 분 $C$ 초에서 $D$ 초 후의 시각을 찾는 문제로 시는 $24$ 단위로, 분과 초는 $60$ 단위로 다음 단위로 올림이 되는 점을 활용해서 몫과 나머지 연산으로 해결했다.


2. 코드


1. 풀이 [Java]

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
import java.io.*;
import java.util.*;

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

        int A = Integer.parseInt(st.nextToken());
        int B = Integer.parseInt(st.nextToken());
        int C = Integer.parseInt(st.nextToken());
        int D = Integer.parseInt(br.readLine());

        C += D;

        B += C / 60;
        C %= 60;

        A += B / 60;
        B %= 60;

        A %= 24;

        System.out.println(A + " " + B + " " + C);
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int a, b, c, d;
    cin >> a >> b >> c >> d;

    c += d;

    b += c / 60;
    c %= 60;

    a += b / 60;
    b %= 60;

    a %= 24;

    cout << a << ' ' << b << ' ' << c;
}

This post is licensed under CC BY 4.0 by the author.