有两个测试数据过不了就大佬帮帮忙(代码写得很丑

P2010 [NOIP2016 普及组] 回文日期

wcj061113 @ 2021-10-14 23:09:34

#include <bits/stdc++.h>
using namespace std;
int d1[4];
int d2[4];
int ans;
int main()
{
    int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int sum1,sum2;
    int day1,day2;
    cin>>day1>>day2;
     d1[3]=day1%100;
     d1[2]=(day1/100)%100;
     d1[1]=day1/10000;

     d2[3]=day2%100;
     d2[2]=(day2/100)%100;
     d2[1]=day2/10000;
     sum1=d1[3]+d1[2]*100+d1[1]*10000;
     sum2=d2[3]+d2[2]*100+d2[1]*10000;
     if(sum1==sum2)
     {
        if(sum1%10==sum1/10000000&&sum1/10%10==sum1/1000000%10&&sum1/100%10==sum1/100000%10&&sum1/10000%10==sum1/1000%10)
        { 

            cout<<1;
            return 0;
         }
         else 
         {
            cout<<ans;
            return 0;
         }
     }
     while(sum1!=sum2)
     {
        if(sum1%10==sum1/10000000&&sum1/10%10==sum1/1000000%10&&sum1/100%10==sum1/100000%10&&sum1/10000%10==sum1/1000%10)
        { 

            ans++;
         }
        if((d1[1]%4==0||d1[1]%100!=0)&&d1[1]%400==0)
        {
            d[2]=29;
         }
         else
         {
            d[2]=28;
         }
        sum1=d1[3]+d1[2]*100+d1[1]*10000;
        d1[3]++;
        if(d1[3]==d[d1[2]]+1)
        {
            d1[3]=1;
            d1[2]++;

        }
        if(d1[2]==13)
        {
            d1[2]=1;
            d1[1]++;
        }

     }
    cout<<ans; 
    return 0;
 } 

by ZYXZYX20110608 @ 2021-11-03 21:56:21

#include<bits/stdc++.h>
using namespace std;

int date1,date2,fan_year,tmp_year,year,aim_date,cnt=0;
int days[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};

int main()
{
    cin>>date1>>date2;
    for(int month=1;month<=12;month++)
    {
        for(int day=1;day<=days[month];day++)
        {
            fan_year=month*100+day;
            tmp_year=fan_year;
            year=0;
            for(int i=1;i<=4;i++)
            {
                year=tmp_year%10+year*10;
                tmp_year/=10;
            }
            aim_date=year*10000+fan_year;
            if(aim_date>=date1 && aim_date<=date2)
            {
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}

|