Post

[BaekJoon] 2753번 - 윤년 [Java][C++]

[BaekJoon] 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++]

C++의 cout에서 기본적으로 true1, false0으로 출력해서 조건식을 바로 출력해도 된다. 이때 연산자 우선순위 때문에 괄호를 쳐줬다.

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

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

    int y;
    cin >> y;
    cout << (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}

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