首页 > 编程学习 > 回文日期(模拟)

回文日期(模拟)

发布时间:2022/5/14 15:54:10

原题链接

思路分析:
模拟即可。
注意 A ! = B A!=B A!=B

#include <bits/stdc++.h>
using namespace std;
int A[8];
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void solve(int S){
    int k = 0;
    while( S > 0 ){
        A[k++] = S % 10;
        S /= 10;
    }
}
bool F1(){
    return A[0] == A[7] && A[1] == A[6] && A[2] == A[5] && A[3] == A[4]; 
}
bool F2(){
    return A[0] == A[2] && A[2] == A[5] && A[5]== A[7] && 
           A[1] == A[3] && A[3] == A[4] && A[4] == A[6] && A[0] != A[6];
}
bool check(int S){
    
    int year = S / 10000;
    
    int month = S / 100 % 100;
    
    int day = S % 100;
    
    if( month < 1 || month > 12 ) return false;
    
    days[2] = 28;
    if( year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) days[2] = 29;
    
    if( day < 1 || day > days[month] ) return false;
    
    return true;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    
    int S;
    cin >> S;
    
    int cnt = 0;
    while( S++ ){
        
        if( check(S) ){
            solve(S);
            if( F1() && cnt == 0 ){
                cout << S << '\n';
                cnt++;
            }
            if( F2() && cnt == 1 ){
                cout << S << '\n';
                cnt++;
            }
        }
        
        if( cnt == 2 ){
            break;
        }
    }
    
    return 0;
}
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000