1.题目链接
2.解题思路
先把前n-1个月的天数加起来 再加最后一天的天数 注意闰年情况
3.代码
#include <iostream>
using namespace std;
int main()
{
static int monthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int year, month, day;
//这样可以不断接收输入 直到按crtl+c就结束
while (cin >> year >> month >> day)
{
int n = 0;
//计算1-(month-1)月的天数
for (int i = 1; i < month; ++i)
{
n += monthDays[i];
}
//累加month那月的天数
n += day;
if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
n += 1;
}
cout << n << endl;
}
return 0;
}
4.运行结果
【C++ OJ练习】2.计算日期到天数转换 完