알고리즘/문제 풀이
BOJ 25286 - 11월 11일 (Javascript)
degurii
2022. 6. 19. 22:17
728x90
문제 링크
https://www.acmicpc.net/problem/25286
25286번: 11월 11일
11월 11일에는 농업인의 날, 가래떡 데이, 보행자의 날, 대한민국 해군 창설 기념일, 유엔참전용사 추모의 날, 빼빼로 데이 등 다양한 의미를 가진 날이다. 성현이는 11월 11일의 11일 전은 10월 31일,
www.acmicpc.net
풀이
JS는 Date 객체를 이용하면 간단하게 풀 수 있습니다.
다만 Date 객체는 기본적으로 로컬 타임에 맞춰버리니 UTC라고 명시해줘야 편합니다.
정답 코드
// @BOJ ------------------------------------------
const fs = require('fs');
const stdin = fs.readFileSync('/dev/stdin').toString().split('\n');
const input = (() => {
let line = 0;
const input = () => stdin[line++];
input.num = () => input().split(' ').map(Number);
input.rows = l => Array(l).fill().map(input);
input.rows.num = l => Array(l).fill().map(input.num);
return input;
})();
// Solution -----------------------------------
const solve = ([year, month]) => {
const date = new Date(Date.UTC(year, month - 1, month));
date.setUTCDate(date.getUTCDate() - month);
return [date.getFullYear(), date.getMonth() + 1, date.getDate()];
};
const solution = function () {
const t = +input();
const ans = input.rows
.num(t)
.map(solve)
.map(ymd => ymd.join(' '))
.join('\n');
console.log(ans);
};
solution();
728x90