秀个花式
2024-05-08 21:03:25
发布于:广东
11阅读
0回复
0点赞
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const static int days[2][12] = {
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31},
};
string s1,s2;
bool isRunYear(int year)
{
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}
int date_date(string s1,string s2)
{
string start = s1 < s2 ? s1 : s2;
string end = s2 > s1 ? s2 : s1;
int startYear,startMonth,startDay;
int endYear,endMonth,endDay;
sscanf(start.c_str(),"%d-%d-%d",&startYear,&startMonth,&startDay);
sscanf(end.c_str(),"%d-%d-%d",&endYear,&endMonth,&endDay);
int day = 0;
for(int y = 1;y < startYear;y++)
day -= isRunYear(y) ? 366 : 365;
for(int y = 1;y < endYear;y++)
day += isRunYear(y) ? 366 : 365;
int gap = isRunYear(startYear);
for(int m = 1;m < startMonth;m++)
day -= days[gap][m-1];
gap = isRunYear(endYear);
for(int m = 1;m < endMonth;m++)
day += days[gap][m-1];
day += endDay - startDay;
if(day < 0) day = 0;
return day;
}
int main()
{
cin >> s1 >> s2;
cout << date_date(s1,s2) << endl;
return 0;
}
这里空空如也
有帮助,赞一个