首先我们说闰年的定义是:
(1)、非整百年能被4整除的为闰年;(如2004年就是闰年,1900年不是闰年)
(2)、整百年能被400整除的是闰年;(如2000年是闰年,1900年不是闰年)
(3)、对于数值很大的年份,这年如果能被3200整除,并且能被172800整除则是闰年。(如172800年是闰年,86400年不是闰年)
代码:IsLeaperYear.c
/************************************
*
* FileName: IsleaperYear.c
* Creator: Smart
* Date: 2014-12-27
*
************************************/
#include <stdio.h>
/*
return the leaper year of two years
*/
int Num_Leapyear(const int a,const int b)
{
unsigned int S=0;
int i;
if(a>b||a<0||b<0)
{
return -1;
}
for (i=a;i<b;i++)
{
if((i%4==0&&i%100!=0)||i%400==0||(i%3200==0&&i%172800==0))
{
S+=1;
}
}
return S;
}
int main()
{
int Temp;
int a,b;
printf("请输入两个年份:");
scanf("%d %d",&a,&b);
Temp=Num_Leapyear(a,b);
if(Temp==-1)
{
printf("error!\n");
}
else
{
printf("%d\n",Temp);
}
return 0;
}
运行结果如图:如上。