求助!!!!!

P2010 [NOIP2016 普及组] 回文日期

yipingze @ 2023-07-05 16:38:16

#include<bits/stdc++.h>
using namespace std;
int ww(string s){
    int i=0,j=s.size();
    getline(cin,s);
    while(i<j){
        if(s[i]==s[j]){
            i++;
            j--;
        }else{
            return 0;
        }
    }
    cout<<"1";
}
bool check(int a){
    if((a%4==0&&a%100!=0)||(a%400==0)){
        return true;
    }else{
        return false;
    }

}
string Date(string date){
    string R;
    int days[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
    int year=(date[0]-'0')*1000+(date[1]-'0')*100+(date[2]-'0')*10+(date[3]-'0');
    int month=(date[4]-'0')*10+(date[5]-'0');
    int day=(date[6]-'0')*10+(date[7]-'0');
    day+=1;
    int run=check(year);
    if(day>days[run][month-1]){
        day=1;
        month+=1;
        if(month>12){
            month=1;
            year+=1;
        }       
    }
    R+=year/1000+'0';
    R+=year/100%10+'0';
    R+=year/10%10+'0';
    R+=year%10+'0';
    R+=month/10+'0';
    R+=month%10+'0';
    R+=day/10+'0';
    R+=day%10+'0';
    return R;
}
int main(){
    string d1,d2;
    cin>>d1>>d2;
    int s=0;
    while(d1!=d2){
        if(ww(d1)){
            s+=1;
        }
        d1=Date(d1);
    }
    if(ww(d1)){
            s+=1;
    }
    cout<<s<<endl;
}

by zqh123b @ 2023-07-05 16:46:03

@yipingze
虽然这个做法似乎也有道理,但是本题可以直接枚举年份,然后计算出月和日,判断月和日是否符合要求即可。当然,也可以枚举月和日,判断年是否在要求的范围内。(这种方法更为简单)
提供一行参考代码:

int year=(day%10)*1000+(day/10)*100+(month%10)*10+(month/10);

by 编码落寞 @ 2023-07-05 16:47:33

@yipingze

#include<bits/stdc++.h>
using namespace std;
int ww(string s){
    int i=0,j=s.size()-1;
    while(i<j){
        if(s[i]==s[j]){
            i++;
            j--;
        }else{
            return 0;
        }
    }
    return 1;
}
bool check(int a){
    if((a%4==0&&a%100!=0)||(a%400==0)){
        return true;
    }else{
        return false;
    }

}
string Date(string date){
    string R;
    int days[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
    int year=(date[0]-'0')*1000+(date[1]-'0')*100+(date[2]-'0')*10+(date[3]-'0');
    int month=(date[4]-'0')*10+(date[5]-'0');
    int day=(date[6]-'0')*10+(date[7]-'0');
    day+=1;
    int run=check(year);
    if(day>days[run][month-1]){
        day=1;
        month+=1;
        if(month>12){
            month=1;
            year+=1;
        }       
    }
    R+=year/1000+'0';
    R+=year/100%10+'0';
    R+=year/10%10+'0';
    R+=year%10+'0';
    R+=month/10+'0';
    R+=month%10+'0';
    R+=day/10+'0';
    R+=day%10+'0';
    return R;
}
int main(){
    string d1,d2;
    cin>>d1>>d2;
    int s=0;
    while(d1!=d2){
        if(ww(d1)){
            s+=1;
        }
        d1=Date(d1);
    }
    if(ww(d1)){
            s+=1;
    }
    cout<<s<<endl;
}

by yipingze @ 2023-07-06 11:26:27

感谢大佬!!!


|