想到了新的思路,记录下来。
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
AC的代码
#include<stdio.h>
#include<stdbool.h>
#define MONTH 30.5 //把二月当30天,然后每个月平均30.5天
bool Year(int x); //判断闰年
int main(void)
{
int year, month, day, n;
while ((scanf("%d/%d/%d", &year, &month, &day)) != EOF)
{
if (month != 9) //经计算发现除九月外,其他月都满足下式。(因为七八月都是大月)
n = MONTH * (month - 1) + day + 0.5;
else
n= MONTH * (month - 1) + day + 0.5+1;
if (Year(year)&&month>2) //如果事闰年,减去一天,因为二月29天
n = n - 1;
else if(month>2)
n = n - 2;
printf("%d\n", n);
}
return 0;
}
bool Year(int x)
{
if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
return true;
else
return false;
}