闰年的两种判断方法:1.能被四整除且不能被100整除;2.能被400整除
答案1:可以使用两个if语句来打印
#include <stdio.h>
int main()
{
int year=0;
for(year=1000;year<=2000;year++)
{
if(year%4==0&&year%100!=0)
{
printf("%d ",year);
}
else if(year%400==0)
{
printf("%d ",year);
}
}
return 0;
}
答案2:将两个if语句合并到一起
即:
if(year%4==0&&year%100!=0)和else if(year%400==0)
变成了:
if((year%4==0&&year%100!=0)||(year%400==0))
#include <stdio.h>
int main()
{
int year=0;
for(year=1000;year<=2000;year++)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
printf("%d ",year);
}
}
return 0;
}
如果想要求总数:
则:
#include <stdio.h>标签:printf,闰年,int,400,2000,year,year%,100,1000 From: https://blog.51cto.com/u_15899086/5967137
int main()
{
int year=0;
int count=0;
for(year=1000;year<=2000;year++)
{
if(year%4==0&&year%100!=0)
{
printf("%d ",year);
count ++;
}
else if(year%400==0)
{
printf("%d ",year);
count++;
}
}
printf("\ncount=%d \n",count);
return 0;
}