다음은 달력에 관한 몇 가지 일반적인 정보입니다 (필요한 경우 좀 더 연구를 해 보셔도 좋습니다).
- 1900년 1월 1일은 월요일이다.
- 4월, 6월, 9월, 11월은 30일까지 있고, 1월, 3월, 5월, 7월, 8월, 10월, 12월은 31일까지 있다.
- 2월은 28일이지만, 윤년에는 29일까지 있다.
- 윤년은 연도를 4로 나누어 떨어지는 해를 말한다. 하지만 400으로 나누어 떨어지지 않는 매 100년째는 윤년이 아니며, 400으로 나누어 떨어지면 윤년이다
20세기 (1901년 1월 1일 ~ 2000년 12월 31일) 에서, 매월 1일이 일요일인 경우는 총 몇 번입니까?
루비나 파이썬은 제공되는 라이브러리가 있어서 비교적 쉽게 풀이가 가능했고
펄 같은 경우 DateTime이라는 모듈을 ppm을 통해 설치해보게 되었음. :)
Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
startDate = datetime(1901,1,1) | |
endDate = datetime(2000,12,31) | |
plusDay = timedelta(days=1) | |
countOfSunday = 0 | |
while True: | |
if startDate == endDate: | |
break; | |
if startDate.weekday() == 6 and startDate.day == 1: #일요일이고 1일 | |
countOfSunday+=1 | |
print (startDate) | |
startDate = startDate + plusDay #하루 증가 | |
print (countOfSunday) |
Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
startDate=Date.new(1901,1,1) | |
endDate=Date.new(2000,12,31) | |
countOfSunday = 0 | |
while (true) | |
if (startDate === endDate) | |
break; | |
end | |
if startDate.wday == 0 and startDate.mday == 1: #일요일이고 1일 | |
countOfSunday+=1 | |
puts startDate | |
end | |
startDate = startDate + 1 #하루 증가 | |
end | |
puts countOfSunday |
Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use DateTime; | |
use strict; | |
use warnings; | |
my $start_date = DateTime->new( | |
year=>1901, | |
month=>1, | |
day=>1, | |
); | |
my $end_date = DateTime->new( | |
year=>2000, | |
month=>12, | |
day=>31, | |
); | |
my $count_of_sunday = 0; | |
while (1) { | |
if(DateTime->compare($start_date, $end_date) == 0) { | |
last; | |
} | |
if ($start_date->day == 1 and $start_date->day_of_week == 7) { | |
$count_of_sunday+=1; | |
print "$start_date"; | |
} | |
$start_date->add(days=>1); | |
} | |
print "$count_of_sunday"; |