题目描述
输入某年某月某日,判断这一天是这一年的第几天?
分析
注意!!!——平年闰年的2月天数是不一样的!!!
判断闰年的方法:四年一闰,百年不闰,四百年再闰
代码解析
#include <stdio.h>
int main()
{
int year,month,day,i,count=0;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
printf("请输入年月日\n");
scanf("%d %d %d",&year,&month,&day);
for(i=0;i<month-1;i++)
if((year%4==0&&year%100!=0)||(year%400==0))
count+=b[i];
else count+=a[i];
count+=day;
printf("这是%d年的%d天",year,count);
return 0;
}
判断闰年这一块也可以利用函数体来进行改进
#include <stdio.h>
int if_leap_year(int year);
int main()
{
int year,month,day,i,count=0;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
printf("请输入年月日\n");
scanf("%d %d %d",&year,&month,&day);
for(i=0;i<month-1;i++)
if(if_leap_year(year))
count+=b[i];
else count+=a[i];
count+=day;
printf("这是%d年的%d天",year,count);
return 0;
}
int if_leap_year(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else return 0;
}
运行结果