관리 메뉴

Silver Library (Archived)

백준 2753번 - JS 본문

CS Library/JavaScript - Data Structure

백준 2753번 - JS

Chesed Kim 2021. 7. 20. 11:53
반응형

const fs = require("fs")
const inputData = fs.readFileSync("/dev/stdin").toString().split(" ").map(val=>+val)
const [a,b] = inputData

 

근간의 메모.

여기에서, 윤년을 구하라고 요구하고 있다.

수정 할 부분은 반복 작업을 위해 참조 될 inputData 을 참조하는 const 변수 식별값 부분.

이렇게 해야만, 입력 시, if 문에 맞는 결과 값을 내놓을 수 있다.

그리고 그 과정을 composing 하기 위해서 필요한게, 변수값 할당이다.

 

문제.

연도가 주어졌을 때, 윤년이면 1, 아니면 0을 출력하는 프로그램을 작성하시오.

윤년은 연도가 4의 배수이면서, 100의 배수가 아닐 때 또는 400의 배수일 때이다.

예를 들어, 2012년은 4의 배수이면서 100의 배수가 아니라서 윤년이다. 1900년은 100의 배수이고 400의 배수는 아니기 때문에 윤년이 아니다. 하지만, 2000년은 400의 배수이기 때문에 윤년이다.

 

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

 

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

 

수정1.

const yearJudicator = inputData

 

문제의 윤년. 한국의 초등학교 까지만 국어를 배운 저로서는 윤년이 뭔지도 몰랐습니다.

 

윤년 - 위키백과, 우리 모두의 백과사전

윤년(閏年)은 역법을 실제 태양년에 맞추기 위해 여분의 하루 또는 월(月)을 끼우는 해이다. 태양년은 정수의 하루로 나누어떨어지지 않고, 달의 공전주기와 지구의 공전주기는 다르기 때문에

ko.wikipedia.org

머리가 아파오기 시작해서 plain English form 으로 다시 진입 했습니다.

다음에 중국어도 좀 배워 봐야 겠습니다.

 

Leap year - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Calendar year containing an additional day A leap year (also known as an intercalary year or bissextile year) is a calendar year that contains an additional day (or, in the case of a l

en.wikipedia.org

그러니까 이 알고리즘을 요약하자면...

 

if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

 

이다.

 

그럼, 어떻게 접근하지? 우선 템플릿을 이용해봤다.

//시작 부분
const fs = require("fs")
const inputData = fs.readFileSync("/dev/stdin").toString().split(" ").map(val=>+val)
const yearJudicator = inputData // yearJudicator 는 식별값으로 쓰일 inputData 의 변수 값이다.

// if (year is not divisible by 4) then (it is a common year)
// else if (year is not divisible by 100) then (it is a leap year)
if (yearJudicator % 4 == 0 && yearJudicator % 100 != 0){
	console.log(1)
}

// if (year is not divisible by 4) then (it is a common year)
// else if (year is not divisible by 400) then (it is a common year)
else if (yearJudicator % 4 == 0 && yearJudicator % 400 == 0){
	console.log(1)
}

// else (it is a leap year)
else
	console.log(0)

 

참고로 && 는 and 가 맞다. || 이게 or 인 것.

 

그리고 파이선은 이렇게 표현이 가능하다고 한다.

a = int(input())
if (a % 4 == 0 and a % 100 != 0) or a % 400 == 0:
	print(1)
else:
	print(0)

 

 

 

참조:

https://helicopter55.tistory.com/49

https://pacific-ocean.tistory.com/72

https://ko.javascript.info/logical-operators

https://en.wikipedia.org/wiki/Leap_year#Algorithm