题目描述
输入年、月、日,计算该天是本年的第几天。
输入描述:
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。
输出描述:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1
输入
1990 9 20
2000 5 1
输出
263
122
解法
#include <stdio.h>
int dayInYear(int year, int month, int day) {
int number = 0;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { //闰年
switch (month) {
case(1): number = day; break;
case(2): number = 31 + day; break;
case(3): number = 31 + 29 + day; break;
case(4): number = 31 + 29 + 31 + day; break;
case(5): number = 31 + 29 + 31 + 30 + day; break;
case(6): number = 31 + 29 + 31 + 30 + 31 + day; break;
case(7): number = 31 + 29 + 31 + 30 + 31 + 30 + day; break;
case(8): number = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; break;
case(9): number = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; break;
case(10): number = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break;
case(11): number = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break;
case(12): number = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break;
}
}
else {
switch (month) {
case(1): number = day; break;
case(2): number = 31 + day; break;
case(3): number = 31 + 28 + day; break;
case(4): number = 31 + 28 + 31 + day; break;
case(5): number = 31 + 28 + 31 + 30 + day; break;
case(6): number = 31 + 28 + 31 + 30 + 31 + day; break;
case(7): number = 31 + 28 + 31 + 30 + 31 + 30 + day; break;
case(8): number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; break;
case(9): number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; break;
case(10): number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break;
case(11): number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break;
case(12): number = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break;
}
}
return number;
}
int main() {
for(int year, month, day; ~scanf("%d %d %d", &year, &month, &day);)
printf("%d", dayInYear(year, month, day));
return 0;
}
网友评论