1WA 3TLE

P2010 [NOIP2016 普及组] 回文日期

HarryPotterJames @ 2021-09-25 16:59:24

#include<bits/stdc++.h>
#define maxn 100 

using namespace std;

string to_String(int n)
{
    int m = n;
    char s[maxn];
    char ss[maxn];
    int i=0,j=0;
    if (n < 0)// 处理负数
    {
        m = 0 - m;
        j = 1;
        ss[0] = '-';
    }    
    while (m>0)
    {
        s[i++] = m % 10 + '0';
        m /= 10;
    }
    s[i] = '\0';
    i = i - 1;
    while (i >= 0)
    {
        ss[j++] = s[i--];
    }    
    ss[j] = '\0';    
    return ss;
}

int main()
{

    long long a,b;
    cin>>a>>b;
    long long ans=0;
    string a1=to_String(a),b1=to_String(b);
    for(int i=a;i<=b;i++)
    {
        string s=to_String(i);
        string s1=s;
        reverse(s.begin(),s.end());
        if(s==s1)
            ans++;
    }
    cout<<ans<<endl;
    return 0;
}

by Deirdre @ 2021-09-25 17:30:04

@HarryPotterJames 用不了那么长

#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,c,sum,ans;
int s[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int main(){
    ios::sync_with_stdio(0);
    scanf("%d%d",&n,&m);
    for(register int i=1;i<=12;i++)
        for(register int j=1;j<=s[i];j++){
            c=(j%10)*1000+(j/10)*100+(i%10)*10+(i/10);
            sum=c*10000+i*100+j; 
            if (sum<n||sum>m) continue;
            ans++; 
        }
    printf("%d",ans);
    return 0;
}

|