카테고리 없음

백준 2753번 :: 윤년

Sun720 2022. 5. 17. 23:43

▶ 문제

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

 

2753번: 윤년

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

www.acmicpc.net

 설명

윤년은 4년에 한번씩 돌아오지만 100년단위일 경우엔 윤년이 아니다. 단, 400년 단위로는 윤년으로 친다.

4년에 한번씩 && 100년 단위는 아님  || 400년 단위

이런 식이 먼저 떠올라 그대로 코드에 옮겨보았다. 

문제 풀이

🌱 풀이1. if문 &&, || 사용.

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

public class Main {

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

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

		int result = 0;

		if ((inputYear % 4 == 0) && (inputYear % 100 != 0) || (inputYear % 400 == 0)) {
			result = 1;
		}

		System.out.println(result);
	}

}

🌱 풀이2. if문 중첩

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

public class Main {

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

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

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

if문을 중첩시켜서 else if와 else 문을 함께 사용해 보았다.

 

Log

728x90
반응형