从1970到指定年份有多少个闰年 快速算法

/*

 * Number of leap years from 1970 up to, butnot including, the specified year

 * (expressed as the number of years since1900).

 */

#define _ELAPSED_LEAP_YEARS(y)  (((y - 1)/4) - ((y- 1)/100) + ((y + 299)/400) \

                                - _LEAP_YEAR_ADJUST)

[1900,x) y=x-1900表示从[1900,x-1]一共有y年

((y -1)/4)表示这y年一共有多少能被4整除的数,除去第一个

((y -1)/100)表示这y年一共有多少能被100整除的数,除去第一个

((y +299)/400)表示一共有多少能被400整除的数

(y-101+400)/400

[1900,2000]共有101年

_LEAP_YEAR_ADJUST为从1900-1970,一共有17个闰年


猜你喜欢

转载自blog.csdn.net/winterain/article/details/7844759
今日推荐