输入年、月、日,计算该天是本年的第几天。
输入描述:
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。
输出描述:
输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1
输入
1990 9 20
2000 5 1
输出
263
122
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
bool isyeap(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true;
return false;
}
int dayofmonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int main() {
int y, m, day, i, count;
while (scanf("%d %d %d", &y, &m, &day) != EOF) {
count = 0;
if (isyeap(y)) {
dayofmonth[1] = 29;
}
else dayofmonth[1] = 28;
for (i = 0; i < m - 1; i++) {
count += dayofmonth[i];
}
count += day;
printf("%d\n", count);
}
return 0;
}
本文介绍了一种计算任意给定日期(年、月、日)为所在年度第几天的方法,通过判断是否为闰年调整二月天数,再累加各月天数直至目标月份,最后加上具体日期即可得出结果。
366

被折叠的 条评论
为什么被折叠?



