原本70分,一改就没输出了!在线求调

P2010 [NOIP2016 普及组] 回文日期

FQY_0307 @ 2024-08-07 19:45:41

#include<iostream>
using namespace std;
int main(){
    int from,to;
    cin>>from>>to;
    int year=from/10000;
    int month=from/100%100;
    int day=from%100;
    int sum=0;
    while(year*10000+month*100+day<=to){
        int a,b,c,d;
        d=month/10%10;
        c=month%10;
        b=day/10%10;
        a=day%10;
        if(d+10*c+100*b+1000*a==year){
            sum++;
        }
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            if(day==31){
                day=0;
                if(month==12){
                    month=1;
                    year++;
                }
            }
        }
        else if(month==2&&day==28){
            if(!((year%4==0&&year%100!=0)||year%400==0)){
                day=0;
                month++;
            }
        }
        else if(month==2&&day==29){
            day=0;
            month++;
        }
        else{
            if(day==30){
                day=0;
                month++;
            }
        }
        day++;
    }
    cout<<sum;
    return 0;
}

感觉可读性还挺高的,大佬帮忙看看,谢谢


by SIRIUS0105 @ 2024-08-07 19:55:50

@SIRIUS0105 话说好久没在讨论区看到这么舒服的马蜂了(逃)


by FQY_0307 @ 2024-08-07 20:01:38

@SIRIUS0105 你的意思是时间超限导致无输出吗?


by liyuan2023 @ 2024-08-07 20:03:12

@FQY_0307

year,month,day的判定有问题。

完整代码如下:

#include<iostream>
using namespace std;
int y[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
    int from,to;
    cin>>from>>to;
    int year=from/10000;
    int month=from%10000/100;
    int day=from%100;
    int sum=0;
    //cout<<year<<" "<<month<<" "<<day<<endl;
    while(year*10000+month*100+day<=to){
        if(year%400==0||year%100!=0&&year%4==0){
            y[2]=29;
        }
        else{
            y[2]=28;
        }
        int a,b,c,d;
        d=month/10;
        c=month%10;
        b=day/10;
        a=day%10;
        if(d+10*c+100*b+1000*a==year){
            sum++;
        }
        if(day+1>y[month]){
            day=0;
            month++;
        }
        if(month>12){
            month=1;
            year++;
        }
        day++;
    }
    cout<<sum;
    return 0;
}

by liyuan2023 @ 2024-08-07 20:04:55

@SIRIUS0105

因为只有8位,所以O(99999999)不会超


by FQY_0307 @ 2024-08-07 20:07:56

@liyuan2023 谢谢


by SIRIUS0105 @ 2024-08-07 20:12:11

@liyuan2023 ok,我下次尽量看清楚【逃】


|