题目:输入某年某月某日,判断这一天是这一年的第几天?
程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
#include<stdio.h>
//某月的天数
int getMonthDay(int month)
{
int day = 0;
switch(month){
case 2:
day = 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
default:
day = 0;
}
return day;
}
//是否闰年
int isleap(int year)
{
if ( (year%4==0 && year%100!=0) || (year%400==0) ){
return 1;
}
return 0;
}
int main()
{
int year, month, day;
printf("请输入年月日(2019-5-1):\n");
scanf("%d-%d-%d", &year, &month, &day);
int days = day, m;
//循环月份到当前月份的上个月
for(m=1; m<month; m++){
days += getMonthDay(m);
}
if (isleap(year)==1 && month>2){
days += 1;
}
printf("%d-%d-%d是这一年的第%d天", year, month, day, days);
return 0;
}