【求救-P2010】如何改进这个程序?

P2010 [NOIP2016 普及组] 回文日期

jingwenMC @ 2018-08-12 16:47:23

先话不多说,上结果

cout<<"1"<<"ac"<<endl;
cout<<"2"<<"tle"<<endl;
cout<<"3"<<"ac"<<endl;
cout<<"4"<<"ac"<<endl;
cout<<"5"<<"ac"<<endl;
cout<<"6"<<"ac"<<endl;
cout<<"7"<<"ac"<<endl;
cout<<"8"<<"ac"<<endl;
cout<<"9"<<"wa"<<endl;
cout<<"10"<<"tle"<<endl;

源代码:

#include<iostream>
using namespace std;
int main()
{
    int date1,date2;
    cin>>date1>>date2;
    if(date1==date2)
{
    int x,y,z;
    x=date1;
    y=0;
    while(x>0)
    {
        z=x%10;
        x=x/10;
        y=y*10+z;
    }
    if(date1==y)cout<<"1"<<endl;
    else cout<<'0'<<endl;
    return 0;
}

    else if (date1 < date2)
    {
        //*
        int dd[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int x, y, z,c=0;
        for (int i = date1; i <= date2; i++)
        {
            int modi=i%10000/100,di=i%100;
            x = i;
            y = 0;
            while (x>0)
            {
                z = x % 10;
                x = x / 10;
                y = y * 10 + z;
            }
            if (i == y&&modi<13&&di<dd[modi])
            {
                c++;
            }
        }
        //if(c==2)c=9;
        cout <<c<< endl;
        return 0;//*/
    }

}

请问该如何改进?


by jingwenMC @ 2018-08-12 16:48:20

顶~


by Smile_Cindy @ 2018-08-12 16:54:12

@jingwenMC

你忘了判date1>date2


by ylxmf2020 @ 2018-08-12 17:39:02

楼上大佬


by CHY123 @ 2018-08-29 09:45:55

@Alpha 题目不是保证了 date1 —定不晚于date2 吗


by Smile_Cindy @ 2018-08-29 19:58:52

@jingwenMC

首先,你如果从date1枚举到date2会有很多重复累赘的情况,其次在date1和date2之间会有不在它们范围内的日期。


by jingwenMC @ 2018-10-26 22:32:09

@Alpha 谢谢!


by cs613 @ 2019-11-16 11:22:24

@jingwenMC 枚举年份就行了


|