Files
python/TangDou/CSP-J/BiShuaTi/meiju/P2010_int.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
1.3 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// https://www.luogu.com.cn/problem/P2010
#include <bits/stdc++.h>
using namespace std;
//月份日期常数数组
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//判断是不是闰年
bool LeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
//是不是回文字符串
bool isHW(string s) {
string t = s;
reverse(t.begin(), t.end());
return s == t;
}
int cnt;
int main() {
int st, ed;
cin >> st >> ed;
int y1 = st / 10000;
int y2 = ed / 10000;
//枚举年份
for (int i = y1; i <= y2; i++)
// 枚举月份
for (int j = 1; j <= 12; j++) {
//枚举日期
int day = months[j];
if (LeapYear(i) && j == 2) day = 29; //闰年二月份,共29天
for (int k = 1; k <= day; k++) {
int s = i * 10000 + j * 100 + k;
//与范围字符串st,ed进行比较,如果不在范围内,continue
if (s < st || s > ed) continue; //日期字符串符合字典序
//判断是不是回文
char x[10];
sprintf(x, "%d", s);
if (isHW(x)) cnt++;
}
}
//输出
printf("%d\n", cnt);
return 0;
}