统计两日期之间相隔天数
输入
两个日期,如
20200205
20200208
输出
4
#include<iostream>
#include<cstdio>
using namespace std;
int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
bool isleap(int year)
{
return (year%4==0&&year%100!=0)||(year%400);
}
int main()
{
int time1,y1,m1,d1;
int time2,y2,m2,d2;
int ans=1;
cin>>time1>>time2;
if(time1>time2)
{
int temp=time1;
time1=time2;
time2=temp;
}
y1=time1/10000,m1=time1%10000/100,d1=time1%100;
y2=time2/10000,m2=time2%10000/100,d2=time2%100;
while(y1<y2||m1<m2||d1<d2){
d1++;
if(d1==month[m1][isleap(y1)]+1)
{
m1++;
d1=1;
}
if(m1==13)
{
y1++;
m1=1;
}
ans++;
}
cout<<ans;
}