sprintf 将数字转化为字符数组十分的方变
C++ 代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <deque>
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}};
int n,yy,mm,dd,m;
int yy2,mm2,dd2;
int res;
int isRun(int y){
if(y%400==0 ||( y%4==0 && y%100!=0)) return 1;
return 0;
}
bool myfun1(int y,int m,int d){
char str[20];
sprintf(str,"%d",y);
sprintf(str+4,"%02d",m);
sprintf(str+6,"%02d",d);
for(int i=0,j=7;i<j;i++,j--){
if(str[i]!=str[j]) return false;
}
return true;
}
int main(){
cin>>n>>m;
dd=n%100,n/=100;
mm=n%100,n/=100;
yy=n;
dd2=m%100,m/=100;
mm2=m%100,m/=100;
yy2=m;
while(yy<yy2 || mm<mm2 || dd<=dd2){
if(dd>month[mm][isRun(yy)]){
dd=1;
mm++;
}
if(mm>=13){
mm=1;
yy++;
}
if(myfun1(yy,mm,dd)) res++;
dd++;
}
cout<<res<<endl;
return 0;
}