萌新求助,TLE了1个点

P2010 [NOIP2016 普及组] 回文日期

xtracer @ 2021-01-27 16:01:33

我用结构体开了个超级大暴力,开了O2才过。。。这道题是教练让我们做的,并且他说今晚要检查,不能有O2字样。请各位大佬帮一下忙,谢谢!

#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
ll str_to_ll(string str){
    ll ans=0;
    for(ll i=0;i<str.length();i++)ans=ans*10+str[i]-48;
    return ans;
}
string ll_to_str(ll n){
    string ans="";
    while(n){
        ans=char(n%10+48)+ans;
        n/=10;
    }
    return ans;
}
class date{
    public:
        ll year,month,day;
        date getdate(ll a,ll b,ll c){
            date ans;
            ans.year=a;
            ans.month=b;
            ans.day=c;
            return ans;
        }
        bool runyear(ll y){
            if(y%400==0)return true;
            else if(y%100!=0 and y%4==0)return true;
            else return false;
        }
        ll dayofmonth(ll y,ll m){
            if(m==2){
                if(runyear(y))return 29;
                else return 28;
            }
            else{
                if(m==1 or m==3 or m==5 or m==7 or m==8 or m==10 or m==12)return 31;
                else return 30;
            }
        }
        void get_date(string str){
            string y=str.substr(0,4);
            string m=str.substr(4,2);
            string d=str.substr(6,2);
            year=str_to_ll(y);
            month=str_to_ll(m);
            day=str_to_ll(d);
        }
        date plus(ll num){
            day+=1;
            ll dom=dayofmonth(year,month);
            if(day>dom){
                day-=dom;
                month+=1;
            }
            if(month>12){
                month-=12;
                year+=1;
            }
            return getdate(year,month,day);
        }
        string add0(ll n){
            if(n>=10)return ll_to_str(n);
            else return '0'+ll_to_str(n);
        }
        void output(){
            cout<<add0(year)<<add0(month)<<add0(day);
        }
};
date makedate(string str){
    string x,y,z;
    ll a,b,c;
    x=str.substr(0,4);
    y=str.substr(4,2);
    z=str.substr(6,2);
    a=str_to_ll(x);
    b=str_to_ll(y);
    c=str_to_ll(z);
    date res={a,b,c};
    return res;
}
bool cmp(date x,date y){
    if(x.year!=y.year)return x.year>=y.year;
    else if(x.month!=y.month)return x.month>=y.month;
    else return x.day>=y.day;
}
bool huiwen(date n){
    string str;
    str+=n.add0(n.year);
    str+=n.add0(n.month);
    str+=n.add0(n.day);
    string rev;
    for(ll i=0;i<str.length();i++)rev+=str[str.length()-i-1];
    return rev==str;
}
string str;
date a,b;
ll ans=0;
int main(){
    cin>>str;
    a=makedate(str);
    cin>>str;
    b=makedate(str);
    for(date i=a;cmp(b,i);i.plus(1)){
        if(huiwen(i))ans+=1;
    }
    cout<<ans;
    return 0;
}

by xtracer @ 2021-01-27 16:27:02

@滑翔翼 谢谢啊


by xfrvq @ 2021-01-27 16:27:48

看了您的代码感觉我太弱了


by xtracer @ 2021-01-27 16:30:49

@滑翔翼 额...为啥要用“您”这个字啊...您是忘了我吗...


上一页 |