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;
}