【问题描述】
定义一个日期结构体类型(包括年、月、日),计算某日在本年中是第几天,注意闰年问题。
【输入形式】一个日期,包括年、月、日
【输出形式】该日期为该年的第几天
【样例输入】
2021 10 27
【样例输出】
300
闰年的判断规则如下:
(1)若某个年份能被 4 整除但不能被 100 整除,则是闰年。
(2)若某个年份能被 400 整除,则也是闰年。
#include<stdio.h>
struct Date
{
int year;
int month;
int day;
};
int main()
{
struct Date time;//定义time为结构名
int Month[12] = { 0,28,31,30,31,30,31,31,30,31,30,31 };//每个月的
int i,sum=0;
scanf("%d %d %d", &time.year, &time.month, &time.day);
for (i = 0; i < time.month; i++)
{
sum += Month[i];//本月之前的月份的和
}
sum += time.day;//直接加上该月的日期——>不用2月29日不用特别考虑
if (time.month >2) {//月份在2月之后才需要考虑闰年
if ((time.year % 4 == 0 && time.year % 100 != 0) || time.year % 400 == 0)
{
sum++;
}
}
printf("%d", sum);
return 0;
}