90分求助!!!

P2010 [NOIP2016 普及组] 回文日期

liyifanglan @ 2024-07-25 14:15:01

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

bool huiwen(int a){
    string n = to_string(a);
    int right, left;
    left = 0;
    right = n.size()-1;
    bool x = 1;
    while (left < right){
        if (n[right] != n[left]){
            x = 0;
            break;
        }
        left++,  right--;
    }
    return x;
}
bool runnian(int n){
    if ((n%4 == 0 && n%100 != 0) || n%400 == 0){
        return 1;
    }else{
        return 0;
    }
}
int a, b, ans = 0;
int an, ay, ar;
int bn, by, br;
int yue[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
    cin >> a >> b;
    an = a/10000, ay = a/100%100, ar = a%100;
    bn = b/10000, by = a/100%100, br = a%100;
    for (int n = an; n <= bn; n++){
        if (runnian(n) == 1){
            yue[2] = 29;
        }else{
            yue[2] = 28;
        }
        for (int y = 1; y <= 12; y++){
            if (n == an && y < ay) continue;
            if (n == bn && y > by) break;
            for (int r = 1; r <= yue[y]; r++){
                if (n == an && y >= ay && r < ar) continue;
                if (n == bn && y <= by && r > br) break;
                int sum = n*10000+y*100+r;
                if (huiwen(sum) == 1) ans++;
            }
        }
    }
    cout << ans;
}

by purplepolarstar @ 2024-07-26 16:50:41

你为什么要把代码写那么长,这道题思路还是蛮简单的呀! 看我的,AC代码

#include<bits/stdc++.h>
using namespace std;
int i,j,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(){
    cin>>n>>m;
    for (i=1;i<=12;i++){
        for (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++;
        }
    }
    cout<<ans;
    return 0;
}

by timmy2012 @ 2024-07-30 20:27:37

@linjingyi 我有个问题,为什么没有判闰年


by purplepolarstar @ 2024-07-30 22:50:08

有公式,不用判闰年 @timmy2012


|