You are given the following information, but you may prefer to do some research for yourself.
- 1 Jan 1900 was a Monday.
- Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine. - A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
이놈의 짧디 짧은 영어... (T_T)
import Data.Time.Calendar
import Data.Time.Calendar.WeekDate
import Data.Maybe
main :: IO()
main = print $ length $ filter (== 1) $ sundayDateList $ sundayList diffdaysList
sundayDateList :: [Integer] -> [Int]
sundayDateList = map (`calDay` toGregorian)
sundayList :: [Integer] -> [Integer]
sundayList = mapMaybe (\x -> if calDay x toWeekDate == 7 then Just x else Nothing)
calDay :: Integer -> (Day -> (Integer, Int, Int)) -> Int
calDay x myFn = take3rd $ myFn $ addDays x $ fromGregorian 1901 1 1
take3rd :: (Integer, Int, Int) -> Int
take3rd (_, _, a) = a
diffdaysList :: [Integer]
diffdaysList = [0..(diffDays (fromGregorian 2000 12 31) (fromGregorian 1901 1 1))]
*Main> main
171
댓글 없음:
댓글 쓰기