80分!!!QAQ

P2010 [NOIP2016 普及组] 回文日期

RaymondA @ 2024-03-16 21:07:27

错最后三个测试点的代码

#include<bits/stdc++.h>
using namespace std;
int a, b, sum = 0;
int d[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
    int nian1, nian2;
    cin >> a >> b;
    nian1 = a / 10000;
    nian2 = b / 10000;
    for(int i = nian1; i <= nian2; i++){
        d[4] = 28;
        int y, r;
        int c = (i % 10) * 1000 + (i / 10 % 10) * 100 + (i / 100 % 10) * 10 + i / 1000;
        int huiwen = i * 10000 + c;
        y = c / 100;
        r = c % 100;
        if(a == b && huiwen == a){
            sum++;
            break;
        }
        if(y <= 12 && y > 0){
            if(i % 400 == 0 || (i % 4 == 0 && i % 100 != 0)){
                d[4] = 29;
            }
            if(r <= d[y] && r > 0) sum++;
        }
    }
    cout << sum;
    return 0;
}

by Zebraj @ 2024-03-16 21:25:13

@RaymondA 我只几道代码怎么写,理解我的吧

#include<bits/stdc++.h>
using namespace std;
int runnian(int x)
{
    if(x%4==0&&x%100!=0)return 1;
    else if(x%400==0)return 1;
    else return 0;
}
int truedate(int x)
{
    int year=x/10000,month=(x/100)%100,day=x%100;
    if(month==0||month>12)return 0;
    if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day>31)return 0;
    if((month==4||month==6||month==9||month==11)&&day>30)return 0;
    if(!runnian(year)&&month==2&&day>28)return 0;
    if(runnian(year)&&month==2&&day>29)return 0;
    return 1;
}
int main()
{
    int x,y,sum=0;cin>>x>>y;int x1=x/10000,y1=y/10000;
    for(int i=x1;i<=y1;i++)
    {
        int n=i*10000+i%10*1000+i%100/10*100+i%1000/100*10+i/1000;
        if(truedate(n)&&n>=x&&n<=y)sum++;
    }
    cout<<sum;
    return 0;
}

|