Post

[백준] 2753번 - 윤년 [Java][C++]

[백준] 2753번 - 윤년 [Java][C++]

문제 링크


1. 문제 풀이

윤년인지 구하는 문제로 $4$ 의 배수이면서 $100$ 의 배수가 아닌 경우와 $400$ 의 배수인 경우 윤년으로 판단하도록 조건식을 구성하면 된다.


2. 코드

1. 구현 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.io.*;

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

        int year = Integer.parseInt(br.readLine());

        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
    }
}

2. 구현 [C++]

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

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

    int y;
    cin >> y;

    if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
        cout << 1;
    } else {
        cout << 0;
    }
}

3. 풀이 정보

1. 구현 [Java]

언어시간메모리코드 길이
Java 11104 ms14396 KB411 B

2. 구현 [C++]

언어시간메모리코드 길이
C++ 170 ms2020 KB255 B

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