c++60分,求调

P2010 [NOIP2016 普及组] 回文日期

LZY280 @ 2024-10-17 21:04:30

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,sum; 
int main()
{
    cin>>n>>m;
    for(int i=n;i<=m;i++){
        if(i%10000<=1231){
            ll h=0,q=0,p=0;
            q=i/10000;
            p=q;
            for(int j=1;j<=4;j++){
                h=h*10+q%10;
                q/=10;
            }
            if(h==i-p*10000)
                sum++;
        }
    }
    cout<<sum;
    return 0;
}

by LIUHAOYU2013 @ 2024-10-19 16:41:15

@LZY280

#include<bits/stdc++.h>
using namespace std;
int getday(int nian,int yue)
{
    if(yue==1 || yue==3 || yue==5 || yue==7 || yue==8 || yue==10 || yue==12)
        return 31;
    if(yue==4 || yue==6 || yue==11 || yue==9)
        return 30;
    if(nian%400==0)
        return 29;
    if(nian%4==0 && nian%100!=0)
        return 29;
    return 28;
}
int l(int nian,int yue,int ri)
{
    if(yue<1 || yue>12)
        return 0;
    int day=getday(nian,yue);
    if(ri<1 || ri>day)  
        return 0;
    return 1;
}
int main()
{
    int a,b,sum=0;
    cin>>a>>b;
    for(int i=a/10000;i<=b/10000;i++)
    {
        int ge=i%10;
        int shi=i/10%10;
        int bai=i/100%10;
        int qian=i/1000%10;
        int yue=ge*10+shi;
        int tian=bai*10+qian;
        if(l(i,yue,tian)==0)
            continue;
        if(i*10000+yue*100+tian<a || i*10000+yue+100+tian>b)
            continue;
        sum++; 
    }
    cout<<sum;
}

|