两个时间段的天数

#include <iostream>
#include <string>
#include <stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int y, m, d;
cin >> y >> m >> d;
int n = 0, n1 = 0;
for (int i = 0; i <= y; i++)
{
if (i % 100 != 0 && i % 4 == 0 || i % 400 == 0)
{
n += 366;
}
else
{
n += 365;
}
}
if (y % 100 != 0 && y % 4 == 0 || y % 400 == 0)
{
a[1] = 29;
}
for (int i = 0; i < m-1; i++)//m-1 这里很重要
{
n = n + a[i];
}
a[1] = 28;
n = n + d;
for (int i = 0; i <=2021; i++)
{
if (i % 100 != 0 && i % 4 == 0 || i % 400 == 0)
{
n1 += 366;
}
else
{
n1 += 365;
}
}
for (int i = 0; i < 9; i++)
{
n1 = n1 + a[i];
}
n1 = n1 + 10;
cout << n1 - n;
return 0;
}

 这个好使,上面的个别数据不行

#include <iostream>
#include <string>
#include <stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int y, m, d;
cin >> y >> m >> d;
int n = 0, n1 = 0;
for (int i = y; i <= 2021-1; i++)
{
if ((i % 100 != 0 && i % 4 == 0) || (i % 400 == 0))
{
n += 366;
}
else
{
n += 365;
}
}
if ((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
{
a[1] = 29;
}
for (int i = 0; i < m-1 ; i++)
{
n = n - a[i];
}
a[1] = 28;
n = n - d;
for (int i = 0; i < 9; i++)
{
n1 = n1 + a[i];
}
n1 = n1 + 10;
cout << n1 + n;
return 0;
}

posted @ 2021-10-19 19:02  Night_Voyager-qaq  阅读(36)  评论(0编辑  收藏  举报
Live2D