BlueMoons
SRM 189 · 2004-03-31 · by lars2520
SRM 189 · 2004-03-31 · by lars2520 · Math, Simulation
Problem Statement
Problem Statement
For the purposes of this problem, there are exactly 29.53 days between two full moons. A blue moon occurs
whenever there is a full moon twice in one calendar month (January, February,
etc). For example, if a full moon occurs at some point on January 1st, then
another one will occur at some point on January 30th or 31st (depending on what
time it occurred on the 1st). The second one is the blue moon.
Given an interval from one month to another, you are to determine how many blue moons there are between those two months, inclusive. To solve this you must know the following:
Your task is to write a method, count, which takes aString , interval, and another
String , fullMoon, as input and determines how many blue moons there are in that
interval. interval will be formatted as "MM/YYYY to MM/YYYY" where "MM"
represents the month (January is "01" and December is "12") and "YYYY"
represents the year. fullMoon will represent a specific time at which there
was a full moon and will be formatted as "DD.DD/MM/YYYY" where "MM" and "YYYY"
are formatted the same as they are in interval. "DD.DD" represents the day of the month,
as a fraction. Thus "01.00" would mean 12 AM on the first day of the month.
"01.50" would be exactly half way through the first day of the month (12 NOON).
Thus, if fullMoon were "05.75/05/2002", this would mean that there was a full
moon precisely 3 quarters of the way through the 5th day of the 5th month of
2002 (6 PM May 5th 2002).
Given an interval from one month to another, you are to determine how many blue moons there are between those two months, inclusive. To solve this you must know the following:
- Every year has 12 months.
- In a normal year, the number of days in each month are, in order starting with January, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
- In leap years, the second month (February) has 29 days instead of 28.
- Leap years occur every year that is divisible by 4 except for years that are divisible by 100 but not divisible by 400. Thus 1996 was a leap year, as was 2000 (2000 is divisible by 400), but not 1700, 1800, or 1900 (1700, 1800 and 1900 are divisible by 100 but not divisible by 400).
Your task is to write a method, count, which takes a
Notes
- If a full moon occurs at 12AM (Midnight) it is part of the day which has just started, not the one that has just finished.
Constraints
- interval will be formatted exactly as "MM/YYYY to MM/YYYY", with no leading, trailing, or extra spaces.
- fullMoon will be formatted exactly as "DD.DD/MM/YYYY", with no leading or trailing spaces.
- Each MM will contain leading 0's, if necessary, to have exactly 2 digits.
- DD.DD will contain leading and trailing 0's if necessary so that it is always formatted as "DD.DD"
- All dates and times will be valid times since 1900, inclusive. Thus "MM" will be between "01" and "12", inclusive, "YYYY" will be between "1900" and "9999" inclusive, and "DD.DD" will be between "01.00" and "31.99" (depending on the month, the upper bound for the day could be lower), inclusive.
Examples
0)
"01/2002 to 05/2002" "28.95/01/2002" Returns: 0
Given that there was a full moon at the specified time, there are no blue moons during this period.
1)
"01/1900 to 12/1999" "28.95/01/2002" Returns: 41
2)
"01/2000 to 12/2002" "28.95/01/2002" Returns: 1
There are two full moons in November (the 11th month) 2001. The second full moon is at 30.89/11/2001.
3)
"01/2002 to 05/2002" "01.00/01/2002" Returns: 2
4)
"01/2002 to 01/2002" "02.46/01/2002" Returns: 1
8)
"01/2002 to 01/2002" "01.00/02/2002" Returns: 0
Note that fullMoon need not be within interval.
Submissions are judged against all 80 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class BlueMoons with a public method int count(string interval, string fullMoon) · 80 test cases · 2 s / 256 MB per case