描述
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度: O(n) ,空间复杂度: O(1)
输入描述:
输入一行,每行空格分割,分别是年,月,日
输出描述:
输出是这一年的第几天
#include<iostream>
using namespace std;
/*
*思路:
* 1. 通过枚举每个月的1号是这一年的第几天,从而进行累加求和即可,其中注意闰年的处理
*/
int main()
{
int year, month, day;
while(cin>>year>>month>>day)
{
int monthDays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
int nday = monthDays[month-1] + day;
if(month > 2 &&
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
nday += 1;
}
cout<<nday<<endl;
}
return 0;
}