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] 2753번 윤년 본문

IT/Java[백준]

백준[Java] 2753번 윤년

빌드이너프 2023. 2. 14. 15:03

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

 

2753번: 윤년

연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성하시오. 윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다. 예를 들어, 2012년은 4의 배수이면서

www.acmicpc.net

문제풀이

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

        int year = sc.nextInt();

        sc.close();

        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            System.out.println(1);
        else
            System.out.println(0);
    }
}
  • 4의 배수일 경우: if (year % 4) == 0
  • 100의 배수가 아닐 경우: if (year % 100 != 0)
  • 400의 배수일 경우: if (year % 400 == 0)
  • 윤년은 100의 배수 또는 400의 배수: if (year % 100 != 0 || year % 400 == 0)
  • 이면서 4의 배수: if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
  • 만약 윤년일 경우 1 출력, 윤년이 아닐경우 0 출력

문제풀이 2

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

public class _2753_2 {
    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);
    }
}

문제풀이 3

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

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

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

        String result = (year%4==0)?((year%400==0)?"1":(year%100==0)?"0":"1"):"0";

        System.out.println(result);
    }
}

 

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

백준[Java] 2884번 알람 시계  (0) 2023.02.14
백준[Java] 14681번 사분면 고르기  (0) 2023.02.14
백준[Java] 9498번 시험 성적  (0) 2023.02.14
백준[Java] 1330번 두 수 비교하기  (0) 2023.02.14
백준[Java] 2588번 곱셈  (0) 2023.02.14