用scanf()输入某年某月某日,判断这一天是这一年的第几天。以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份≥3时需考虑多加一天。注:判断年份是否为闰年的方法:为400的倍数为闰年,如2000年;若非100的倍数,而是4的倍数,为闰年,如1996年。
#include<stdio.h>
int main(){
int common_year=0,leap_year=1;
int year,month,day,sum;
int common_leap_year[2][13]={{0,31,28,31,30,31,30,31,31,30,31,0,31},
{0,31,29,31,30,31,30,31,31,30,31,0,31}};
printf("please input year-month-day:");
scanf("%d-%d-%d",&year,&month,&day);
if ((year%4==0&&year%100!=0)||year%400==0)//leap_year
{
//天数加和
for (int i = 1; i < month; i++)
{
sum+=common_leap_year[leap_year][i];
}
sum=sum+day;
}else{ //common_year
//天数加和
for (int i = 1; i < month; i++)
{
sum+=common_leap_year[common_year][i];
}
sum=sum+day;
}
printf("It is the %dth day",sum);
return 0;
}