Friday, June 10, 2011

EFI interview - 5

问题:怎样判断一个年份是否是闰年?

这个问题也栽了,当时对if-else的条件捣鼓了半天,还是没有捣鼓对。感觉上,面试官对判断闰年与否的条件给得不是很清楚,算是找个借口吧。

http://support.microsoft.com/kb/214019

In the Gregorian calendar, a normal year consists of 365 days. Because the actual length of a sidereal year (the time required for the Earth to revolve once about the Sun) is actually 365.25635 days, a "leap year" of 366 days is used once every four years to eliminate the error caused by three normal (but short) years. Any year that is evenly divisible by 4 is a leap year: for example, 1988, 1992, and 1996 are leap years.

However, there is still a small error that must be accounted for. To eliminate this error, the Gregorian calendar stipulates that a year that is evenly divisible by 100 (for example, 1900) is a leap year only if it is also evenly divisible by 400.

For this reason, the following years are not leap years: 1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600. This is because they are evenly divisible by 100 but not by 400.

The following years are leap years: 1600, 2000, 2400. This is because they are evenly divisible by both 100 and 400.

To determine whether a year is a leap year, follow these steps:

1). If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2). If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3). If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4). The year is a leap year (it has 366 days).
5). The year is not a leap year (it has 365 days).

这里的算法逻辑很清楚。


http://en.wikipedia.org/wiki/Leap_year

if year modulo 400 is 0
    then is_leap_year
else if year modulo 100 is 0
    then not_leap_year
else if year modulo 4 is 0
    then is_leap_year
else
    not_leap_year

这里的算法非常简洁。

No comments:

Post a Comment