閏年計(jì)算邏輯:能夠被 4 整除的年份是閏年牍汹,但其中能夠被 100 整除的卻不是閏年锌蓄,除非它同時(shí)能被 400 整除。
實(shí)現(xiàn)邏輯:
#include <stdio.h>
int is_leap_year(int a) {
int leap_year = 0;
if (a % 4 == 0)
{
leap_year = 1;
if (a % 100 ==0)
{
leap_year = 0;
if (a % 400 == 0)
{
leap_year = 1;
}
}
}
return leap_year;
}
int main() {
int a[6] = {2000, 1990, 2010, 1900, 2012, 2002};
for (int i = 0; i <6 ; ++i)
{
if (is_leap_year(a[i]))
{
printf("%d is leap year\n", a[i]);
} else {
printf("%d is not leap year\n", a[i]);
}
}
return 0;
}
運(yùn)行:
? C ./a.out
2000 is leap year
1990 is not leap year
2010 is not leap year
1900 is not leap year
2012 is leap year
2002 is not leap year