蓝桥杯 回文日期

https://www.acwing.com/problem/content/2870/

#include <iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<stdlib.h>
#include<unordered_map>
typedef long long LL;
using namespace std;
int y,m,d;
int flag1=1,flag2=1;
string ans1,ans2;
int isr(int x){//????
    return x%400==0 || (x%4==0 && x%100!=0);
}
int cnt=0;
/*
1 3 5 7 8 10 12
2 4 6 9 11
*/
//判断回文
int fun1(string s){
    int b=s.size()-1;
    int a=0;
    int flag=1;
    while(a<=b){
       if(s[a]!=s[b]){
        flag=0;
        break;
       }
       a++;
       b--;
    }
    return flag;
}
//判断ABABBABA
int fun2(string s){
    if(s[0]==s[1]) return 0;
    int flag=1;
    string temp=s.substr(0,2);
    int a=0,b=s.size()-1;
    int cnt=0;
    while(a<b){
        if(s[a]!=s[b] || s[a]!=temp[cnt%2]){
            flag=0;
            break;
        }
        cnt++;
        a++;
        b--;
    }
    return flag;
}
int main(){
    string s;
    cin>>s;
    y=atoi(s.substr(0,4).c_str());
    m=atoi(s.substr(4,2).c_str());
    d=atoi(s.substr(6,2).c_str());
    while(flag1 || flag2){
        d++;
        if(d==32 && m==12){
            m=1;
            d=1;
            y++;
        }
        else if(d==32 && (m==1 || m==3 || m==5 || m==7 || m==8 || m==10)){
            d=1;
            m++;
        }
        else if(d==31 && (m==4 || m==6 || m==9 || m==11)){
            d=1;
            m++;
        }
        else if(d==30 && isr(y) && m==2){
            d=1;
            m++;
        }
        else if(d==29 && !isr(y) && m==2){
            d=1;
            m++;
        }
        string sy=to_string(y);
        string sm="";
        if(m<10) sm="0";
        sm+=to_string(m);
        string sd="";
        if(d<10) sd="0";
        sd+=to_string(d);
        string temp=sy+sm+sd;
        if(fun1(temp) &&flag1){
            ans1=temp;
            flag1=0;
        }
        if(fun2(temp) && flag2){
            ans2=temp;
            flag2=0;
        }
    }
    cout<<ans1<<endl;
    cout<<ans2<<endl;
  return 0;
}
//  freopen("testdata.in", "r", stdin);

posted @ 2021-04-16 16:32  一个经常掉线的人  阅读(61)  评论(0)    收藏  举报