不知道为什么少算了一天,希望各位能帮忙解答,谢谢~
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
int monthDay(int m,bool isL)
{
if(m==2)
{
return isL?29:28;
}
else if(((m<8)&(m%2==1))|((m>7)&(m/2==0)))
{
return 31;
}
else
return 30;
}
bool isLeap(int y)
{
if(((y/4==0)&(y%100))|(y/400==0))
return true;
return false;
}
bool isLoop(int a)
{
string s = to_string(a);
for(int i=0; i<4; ++i)
{
if(s[i]!=s[7-i]) return false;
}
return true;
}
int main()
{
int d1,d2;
cin>>d1>>d2;
int y1 = d1/10000, y2 = d2/10000;
int m1 = (d1/100)%100, m2 = (d2/100)%100;
int r1 = d1%100, r2 = d2%100;
int ms = m1;
int ds = r1;
int c =0;
#不同年份
for(int y=y1; y<y2; ++y)
{
for(int m=ms; m<13; ++m)
{
for(int d = ds; d<=monthDay(m,isLeap(y)); ++d)
{
if(isLoop(++d1)) ++c;
}
d1 = d1 - monthDay(m,isLeap(y1)) + 100;
ds=1;
}
d1 = d1 -1200 + 10000;
ms=1;
}
#相同年份,不同月份
for(int m=ms; m<m2; ++m)
{
for(int d = ds; d<=monthDay(m,isLeap(y1)); ++d)
{
if(isLoop(d1++)) ++c;
}
d1 = d1 - monthDay(m,isLeap(y1)) + 100;
ds=1;
}
#相同月份
for(int d = ds; d<=r2; ++d)
{
if(isLoop(d1++)) ++c;
}
cout<<c;
return 0;
}