91分求助!!c++

P2010 [NOIP2016 普及组] 回文日期

YangjinRun @ 2022-09-09 22:05:06

#include<bits/stdc++.h>
using namespace std;
string a,b,z,s;
int sum,days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31},i,j,k,zmonth,zday,n,m;
int num_year(string a){
    int temp=0;
    for(int i=0;i<4;i++) temp=temp*10+(a[i]-'0');
    return temp;
}
int pd_month(int month,int f){
    if(f==1 && month==2) return 29;
    else return days[month];
}
string hw_year(int year){
    string s;
    while(year>0){
        s+=char(year%10+'0');
        year/=10;
    }
    return s;
}
int main(){
    cin>>a>>b;
    n=num_year(a);
    m=num_year(b);
    for(i=n;i<=m;i++){
        z=hw_year(i);
        //回文年 
        bool f=0;
        if((i%4==0 && i%100!=0) || i%400==0) f=1;
        //判断闰年
        if(i==n) j=(int(a[4])-'0')*10+(int(a[5])-'0');
        if(i==m) zmonth=(int(b[4])-'0')*10+(int(b[5])-'0');
        if(i!=n && i!=m){
            j=1;
            zmonth=12;
        }
        //特判起始月和结束月 
        for(;j<=zmonth;j++){
            if(j<=9) s[0]='0',s[1]=char(j+'0');
            else s[0]=char(j/10%10+'0'),s[1]=char(j%10+'0');
            //字符串加上月 
            if(j==n) k=(int(a[6])-'0')*10+(int(a[7])-'0');
            if(j==m) zday=(int(b[6])-'0')*10+(int(b[7])-'0');
            if(j!=n && j!=m){
                k=1;
                zday=pd_month(j,f);
            }
            //特判起始天和结束天 
            for(;k<=zday;k++){
                if(k<=9) s[2]='0',s[3]=char(k+'0');
                else s[2]=char(k/10%10+'0'),s[3]=char(k%10+'0');
                //字符串加上天
                bool flag=true;
                for(int o=0;o<4;o++){
                    if(z[o]!=s[o]) flag=false;
                }
                if(flag) sum++;
                //判断是否回文 
                //cout<<z<<" "<<s[0]<<s[1]<<s[2]<<s[3]<<endl;
            }
        }
    }
    cout<<sum;
    return 0;
}

by metaphysis @ 2022-09-10 12:37:31

@YangjinRun

代码实现不细致。

//if(i==n) j=(int(a[4])-'0')*10+(int(a[5])-'0');
//if(i==m) zmonth=(int(b[4])-'0')*10+(int(b[5])-'0');

if(i==n) j=(int(a[4])-'0')*10+(int(a[5])-'0'), zmonth = 12;
if(i==m) j = 1, zmonth=(int(b[4])-'0')*10+(int(b[5])-'0');

by YangjinRun @ 2022-09-12 08:12:38

@metaphysis 谢谢大佬!!改完就过了!!


|