第几天?
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
代码如下
#include <stdio.h>
#include <stdlib.h>
int Year[12]={31,28,31,30,31,30,31,31,30,31,30,31};
// 判断是否是闰年
int IsLeapYear(int year){
int re = 0;
if(year%4 == 0 && year%100 != 0)
re = 1;
if(year%400 == 0)
re = 1;
return re;
}
int main(int argc, char *argv[]){
int year, month, day;
int i, re;
while(scanf("%d/%d/%d", &year, &month, &day) != EOF){
i = 0; // 每次循环重新累加
// 如果是闰年且月份大于2 那么day+1
if(IsLeapYear(year))
if(month > 2)
day++;
// 累加每个月的天数,当month等于1月的时候不需要累加,直接输出天数
while(i < (month-1) && month!=1){
day += Year[i];
i++;
}
printf("%d\n", day);
}
return 0;
}
闰年求解
普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年,1999年不是闰年);
世纪闰年:能被400整除的为世纪闰年。(如2000年是闰年,1900年不是闰年);