输入年月日,判断这一天为这一年的第几日
思想:
注意两个点,一个是否为闰年,闰年的二月有29天,二个是否是30天还是31天
#include<stdio.h>
struct S
{
int year;
int month;
int day;
};
int run(int n)
{
if ((n % 4 == 0 && n % 100 != 0) && (n % 400 == 0))
{
return 1;
}
return 0;
}
int pai(int m)
{
switch (m)
{
case 1:
return 31;
case 2:
return 28;
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
}
return 0;
}
int main()
{
struct S s = { 0 };
scanf("%d %d %d",&s.year,&s.month,&s.day);
int ret=run(s.year);
int sum = 0;
int m=0;
int i = 0;
for (i = 1; i <s.month; i++)
{
m = pai(i);
sum = sum + m;
}
sum = sum + s.day;
if (ret == 1)
{
sum = sum + 1;
}
printf("%d\n", sum);
return 0;
}