1.题目:
2.分析:
1. 通过枚举每个月的1号是这一年的第几天,从而进行累加求和即可,其中注意闰年的处理
3.我的代码:
#include <iostream>
using namespace std;
int main()
{
int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int year = 0;
int month = 0;
int day = 0;
int days = 0;
scanf("%d %d %d", &year, &month, &day);
for (int i = 1; i < month; i++)
{
days += arr[i];
}
days += day;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (month > 2)
{
days++;
}
}
printf("%d", days);
return 0;
}
4.答案代码:
#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;
}