Reference:
XL:判断年份是否为闰年的方法Character Set要设定为Use Multi-Byte Character Set。
复制程式
#include "stdafx.h"
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
int nCountYear = 0; //目前年份
int nTotalLeapYearNum = 0; //总闰年个数
int nTotalLeapYearSum = 0; //所有闰年年份总和
int nStartYear = 0; //开始判定年份
int nEndYear = 2006; //最后判定年份
printf("====== Start to show the Leap year between %d and %d =====\n", nStartYear, nEndYear);
//NOTE:每年都判定
for ( int nCountYear = nStartYear ; nCountYear <= nEndYear ; nCountYear++ )
{
if ( ((nCountYear%4 == 0) && (nCountYear%100 != 0)) || //4的倍数,且不为100的倍数。 For 0~399
((nCountYear%100 == 0) && (nCountYear%400 == 0) ) //100的倍数,且为400的倍数。 For 400~???
)
{
++nTotalLeapYearNum; //计算年份个数
nTotalLeapYearSum += nCountYear; //计算年份总合
if ( nTotalLeapYearNum%10 != 0 )
{
printf("%04d ", nCountYear);
}else{
printf("%04d\n", nCountYear);
}
}
} //End of for ( int nCountYear = nStartYear ; nCountYear <= nEndYear ; nCountYear++ )
printf("\n====== End to show the Leap year between %d and %d =====\n", nStartYear, nEndYear);
printf("\n====== Statistics Data ======\n");
printf("There are %d leap years.\n", nTotalLeapYearNum);
printf("The sum of leap years is %d.\n", nTotalLeapYearSum);
::system("pause");
return 0;
}