70分求助!

P2010 [NOIP2016 普及组] 回文日期

Zm120308 @ 2023-03-25 09:03:35

#include<bits/stdc++.h>
using namespace std;
int c[13]={0,31,30,29,30,31,30,31,31,10,31,30,31};
bool e(int i)
{
    int o=0,temp=i;
    while(temp!=0)
    {
        o=o*10+temp%10;
        temp/=10;
    }
    return i==o;
}
int main()
{
    int a,b,cnt=0;
    cin>>a>>b;
    if(e(a)==true)
        cnt++;
    int y=a/10000,m=a/100%100,d=a%100,num=0;
    while(num<b)
    {
        d++;
        if(d>c[m])
        {
            m++;
            d=1;
        }
        if(m>12)
        {
            m=1;
            y++;
        }   
        num=y*10000+m*100+d;
        if(num<=b&&e(num)==true)cnt++;
    }
    cout<<cnt;
    return 0;
}

by SpecialEggs @ 2023-03-25 09:16:34

#include<iostream>
using namespace std;
int d1,d2,cnt;
int y1,y2;
int mon[15]={0,31,0,31,30,31,30,31,31,30,31,30,31};
int main(){
    cin>>d1>>d2;
    y1=d1/10000;
    y2=d2/10000;
    for(int i=y1;i<=y2;i++){
        int m=i%10*10+i/10%10;
        int d=i/100%10*10+i/1000;
        if(i%4==0&&i%100!=0||i%400==0){
            mon[2]=29;
        }else{
            mon[2]=28;
        }
        if(m>=1&&m<=12&&d<=mon[m]&&d>0){
            int tmp=i*10000+m*100+d;
            if(tmp>=d1&&tmp<=d2){
                cnt++;
            }
        }
    }
    cout<<cnt;
    return 0;
}

|