TopCoder problem "CalendarISO" used in SRM 163 (Division I Level Three)



Problem Statement

    

According to the International Organization for Standardization (ISO), the first calendar week of a year is the one that includes the first Thursday of that year, and the last calendar week of a year is the week immediately preceding the first calendar week of the next year. It follows from this definition that some years have 52 calendar weeks, while others have 53. The calendar weeks are numbered in succession from 1 to 52 or 53, as the case may be. Each week begins on a Monday and ends on a Sunday. Observe that a week may spill over from one year into another. For example, if January 1 of some year is a Wednesday, then ISO week 1 of that year includes the last Monday and Tuesday of the previous year. Similarly, if December 31 of some year is a Saturday, then the last ISO week of that year includes the first Sunday of the following year.

The months of April, June, September, and November are 30 days long. The others have 31 days, with the exception of February, which has 29 days in leap years and 28 days otherwise. A leap year is one that is divisible by 4, except if it is divisible by 100 and not divisible by 400. For example, 1996 and 2000 are leap years, but 2099 and 2100 are not. These rules were introduced by Pope Gregory XIII (hence the name "Gregorian calendar") on October 15, 1582, and are part of the calendar standard promulgated today by the ISO.

Mathematicians and programmers have been familiar with this standard for some time, and have developed efficient methods to calculate ISO week numbers. It is rumored, however, that all the library routines are about to become obsolete. Your newspaper's gossip column states that the ISO council, at its next annual congress in Geneva, will announce a three-day shift in the mapping of dates to weekdays, effective retroactively and into the future. Under this ruling, September 8, 2003 ceases to be a Monday and will henceforth be a Thursday, with all other dates remapped to agree with the order of the weekdays. The ISO standard will remain unchanged in every other respect.

Given three int values specifying the year, month, and day of a date between the introduction of the Gregorian calendar and the last day of the year 9999, inclusive, calculate the ISO number of the week within which it falls under the new mapping.

 

Definition

    
Class:CalendarISO
Method:weekNumber
Parameters:int, int, int
Returns:int
Method signature:int weekNumber(int year, int month, int day)
(be sure your method is public)
    
 

Notes

-Months, days, and ISO weeks are all numbered starting from 1.
 

Constraints

-year is between 1582 and 9999, inclusive
-month is between 1 and 12, inclusive
-day is valid for the given month and year
-the given date is between October 15, 1582 and December 31, 9999, inclusive
 

Examples

0)
    
1642
12
25
Returns: 51
When Isaac Newton was born on December 25, 1642, people called it Thursday. With the mapping of weekdays specified above, the same date becomes a Sunday. Therefore, the last ISO week of 1642 begins on Monday, December 26, 1642, and ends on Sunday, January 1, 1643. Because 1642 doesn't have 53 weeks under this mapping, its last week has ISO number 52. In consequence, the immediately preceding Sunday must fall in ISO week 51.
1)
    
1815
2
26
Returns: 9
Napoleon escaped from the isle of Elba on February 26, 1815.
2)
    
2000
1
1
Returns: 1
The Y2K bug failed to cripple the world economy on January 1, 2000.
3)
    
2008
8
8
Returns: 32
The 29th Olympic Games will open in Beijing on August 8, 2008.
4)
    
2276
7
4
Returns: 27
July 4, 2276 will be the quincentenary of the Declaration of Independence.
5)
    
2073
12
31
Returns: 1
6)
    
2006
12
29
Returns: 1
7)
    
9995
12
29
Returns: 1

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1811

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4620&pm=1811

Writer:

Eeyore

Testers:

lbackstrom , brett1479

Problem categories:

Search