跳转至

P1000 回文日期

洛谷 P2010 [NOIP 2016 普及组] 回文日期

题解

本题直接枚举即可,这里给出一个枚举方式较简便的方法。

枚举日期(这里日期不包括年份),年份即为日期颠倒,判断是否在范围内即可。发现日期 92200229 合法,所以我们并不需要判断闰年。这样省去了判断回文和日期是否合法的代码。

参考代码
#include <bits/stdc++.h>
using namespace std;
const int day[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int atoi(string s){
    int k = 0;
    for(auto i : s){
        k = k * 10 + i - '0';
    }
    return k;
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0);
    int a, b;
    cin >> a >> b;
    int cnt = 0;
    for(int i = 1; i <= 12; i++)
        for(int j = 1; j <= day[i]; j++){
            int k = j % 10 * 10000000 + j / 10 * 1000000
                    + i % 10 * 100000 + i / 10 * 10000
                    + i / 10 * 1000 + i % 10 * 100
                    + j / 10 * 10 + j % 10;
            if(a <= k && k <= b)
                cnt++;
        }

    cout << cnt;

    return 0;
}