Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the day of the thirteenth of every month over a period of years

Tags:

c++

calendar

I am currently trying to solve some problems from the USACO training website in preparation for an unrelated C++ programming competition.

However, I am stuck on this problem:

Does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.

The number N is provided in an input file and the output is to be a file with seven numbers in it, each representing the number of 13th's falling on a particular day of the week.

I was wondering how you guys would approach this problem. I am not looking for code or anything since that would just defeat the purpose of me doing this, instead just a starting point or an algorithm would be helpful.

So far the only thing I could think of is using the Doomsday Algorithm, however I am unsure about how I would implement that in code.

Any help would be greatly appreciated.

like image 254
chandsie Avatar asked Feb 26 '23 21:02

chandsie


2 Answers

As Denny says, N is so small that you can easily iterate through the months using a table of days-in-a-month and a simple is-a-leap-year predicate to handle February. Just find out what day the 13th of Jan was in 1900 and then add up the elapsed days until 13th Feb, then 13th March etc.. Use a % operator to wrap the # of elapsed days back into a day-of-week value.

like image 178
Tony Delroy Avatar answered Apr 27 '23 03:04

Tony Delroy


N is less than 400? well you just need to go over 365.25*400=146100 days at max. sounds easy to enumerate all of them, convert dates into year/month/date (with your favorite date conversion routine), testing for day of week is trivial.

I would precalculate the table though.

like image 25
DennyRolling Avatar answered Apr 27 '23 04:04

DennyRolling