求大神指导 c++

P2010 [NOIP2016 普及组] 回文日期

LuvSosa @ 2016-11-28 21:47:08

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{ int a,b,i,j,k,ans;
  int hw[10001]={0},riqi[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  scanf("%d %d",&a,&b);
  i=a/10000;
  j=b/10000;
  ans=j-i+1;
  for(k=1;k<=j-i+1;k++)
  { hw[k]=i/1000+i/100%10*10+i/10%10*100+i%10*1000;
    hw[k]+=i*10000;
    if(i%10+i/10*10%10>12 && i/100%10+i/1000*10>riqi[i%10+i/10%10*10])
    { ans--;
    }
    i++;
  }
  printf("%d",ans);
  return 0;
}

by Secenary @ 2016-11-29 20:39:32

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int f[10];
bool pd(int x){
    if(x%4==0 && x%100!=0)return true;
    if(x%400==0)return true;
    return false;
}
bool hw(int x){
    int s=0,y=x;
    while(x>0){
        s=s*10+x%10;
        x/=10;
    }    
    if(s==y)return true;
    else return false;
}
int main(){
    int i,j,k,m,n,ans=0;
    int s,t,qy,qm,qd,zy,zm,zd;
    cin>>s>>t;
    qy=s/10000;zy=t/10000;
    qm=s%10000/100;    zm=t%10000/100;
    qd=s%100;zd=s%100;
    for(i=qy;i<=zy;i++)
        for(j=1;j<=12;j++){    
            if(i==qy && j<qm)continue;
            if(i==zy && j>zm)break;
            m=d[j];
            if(pd(i) && j==2)m++;
            for(k=1;k<=m;k++){                
                int sum=i*10000+j*100+k;
                if(hw(sum))ans++;
            }    
        }
    cout<<ans<<endl; 
    return 0;
}

by Secenary @ 2016-11-29 20:42:06

暴枚即可


|