题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#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, n = 0;
//在oj题中后台会有多组测试用例
//所以外面这里最好是用while形成多组输入
//这样可以不断接收输入,直到按crtl+c就结束
while ( cin >> year >> month >> day)
{
//先将n(天数)加到该月的前面
for (int i = 1; i < month; i++)
{
n += Monthdays[i];
}
//其次加上该月现在是多少日
n += day;
//判断一下加至三月时是否为闰年,加到三月时已经加完了2月的天数
//是闰年就加一
if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
n += 1;
}
cout << n << endl;
}
}

查看25道真题和解析