[BOJ, JavaScript] 백준 1924번: 2007년
📌 출처
📌 문제
오늘은 2007년 1월 1일 월요일이다. 그렇다면 2007년 x월 y일은 무슨 요일일까? 이를 알아내는 프로그램을 작성하시오.
📌 입력
첫째 줄에 빈 칸을 사이에 두고 x(1 ≤ x ≤ 12)와 y(1 ≤ y ≤ 31)이 주어진다. 참고로 2007년에는 1, 3, 5, 7, 8, 10, 12월은 31일까지, 4, 6, 9, 11월은 30일까지, 2월은 28일까지 있다.
📌 출력
첫째 줄에 x월 y일이 무슨 요일인지에 따라 SUN, MON, TUE, WED, THU, FRI, SAT중 하나를 출력한다.
📌 예제 입출력

📌 코드
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split(" ");
const month = Number(input[0]);
const day = Number(input[1]);
const m = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const week = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
let cnt = 0;
for (let i=0; i<month; i++) {
cnt += m[i];
}
cnt += day;
console.log(week[cnt%7]);
📌 다른 코드
Date()를 사용한 코드입니다.
const fs = require('fs');
const input = fs.readFileSync("/dev/stdin").toString().trim().split(" ");
const month = Number(input[0]);
const day = Number(input[1]);
const week = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
console.log(week[new Date(`2007-${month}-${day}`).getDay()]);
📓 개인 기록용 블로그입니다.
오류가 있을 경우 언제든지 댓글 혹은 메일로 말씀해주시면 감사하겠습니다!
Leave a comment