3번 윤년

문제

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

 

입력

첫째 줄에 연도가 주어진다. 연도는 1보다 크거나 같고, 4000보다 작거나 같은 자연수이다.

 

출력

첫째 줄에 윤년이면 1, 아니면 0을 출력한다.

 

연도가 4의 배수 && (100의 배수가 아닐 때  || 400의 배수일 때)

 

C++

#include <iostream>
using namespace std;
int main(){
    int input;
    cin >> input;
    char result;
    if ((input % 4) == 0 && ((input % 100) != 0 || (input % 400) == 0))
        result = '1';
    else
        result = '0';
    cout << result;
    return 0;
}

 

배수인지 확인할 때는 나머지가 0인지를 확인하면 된다.

 

C#

using System;
class Program{
    static void Main(string[] args){
        int input = int.Parse(Console.ReadLine());
        char result;
        if ((input % 4) == 0 && ((input % 100) != 0 || (input % 400) == 0))
            result = '1';
        else
            result = '0';
        Console.WriteLine(result);
    }
}

 

Python

inputData = int(input());
if inputData % 4 == 0 and (inputData % 100 != 0 or inputData % 400 == 0):
    result = '1';
else:
    result = '0';
print(result);

 

파이썬의 논리 연산자는 and, or, not으로 작성한다.

 

Node.js

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim();
const year = parseInt(input, 10);
let result;
if ((input % 4) == 0 && ((input % 100) != 0 || (input % 400) == 0))
    result = '1';
else
    result = '0';
console.log(result);

 

728x90
반응형

+ Recent posts