C++ 彰雲嘉區複賽 106-5 回文日期問題

題目連結:e525: 106 彰雲嘉區複賽 – Q5 回文日期問題

窮舉法

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int dom[15]={0,31,28,31,30,31,30,31,31,30,31,30,31,0,0};
int pro[700],nt,ct=0;

void check(int y){
    if(y%400==0 || (y%4==0 && y%100!=0)){
        dom[2]=29;
    }else dom[2]=28;
}

void isv(int a){
    string s;
    int b=a;
    while(a>0){
        s+=char(a%10+'0');
        a/=10;
    }
    for(int i=0;i<=s.size()/2;i++){
        if(s[i]!=s[s.size()-1-i]){
            return;
        }
    }
    pro[nt++]=b;
    return;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);

    int n;
    cin>>n;
    while(n--){
        int y;
        nt=ct=0;
        cin>>y;
        check(y);
        for(int i=1;i<=12;i++){
            int tmp=i;
            for(int j=1;j<=dom[i];j++){
                if(i<10){
                    if(j<10){
                        isv(y*100+i*10+j);
                        isv(y*1000+i*10+j);
                        isv(y*1000+i*100+j);
                        isv(y*10000+i*100+j);
                    }else{
                        isv(y*1000+i*100+j);
                        isv(y*10000+i*100+j);
                    }
                }else{
                    if(j<10){
                        isv(y*1000+i*10+j);
                        isv(y*10000+i*100+j);
                    }else{
                        isv(y*10000+i*100+j);
                    }
                }
            }
            i=tmp;
        }
        cout<<nt<<" ";
        sort(pro,pro+nt);
        for(int i=0;i<nt;i++){
            cout<<pro[i]<<" ";
        }
        cout<<"\n";
    }
    return 0;
} 

發佈留言