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

54 lines
1.8 KiB
C++
Raw 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;
}
//通过年月日的整数类型值,构建出一个字符串的年月日
//数字拼接出字符串办法
string build(int year, int month, int date) {
char x[10]; //不太会使用这个char数组,因为最后一个位置需要放置\0,所以需要开大一点点
sprintf(x, "%04d%02d%02d", year, month, date); //拼接出一个字符串
return x; //将字符数组转为字符串,直接赋值即可
}
int cnt;
int main() {
string st, ed;
cin >> st >> ed;
int y1 = stoi(st.substr(0, 4)); //截取字符串,stoi:字符串转数字
int y2 = stoi(ed.substr(0, 4));
//枚举年份
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++) {
//合法日期,构建成字符串
string s = build(i, j, k);
//与范围字符串st,ed进行比较,如果不在范围内,continue
if (s < st || s > ed) continue; //日期字符串符合字典序
//判断是不是回文
if (isHW(s)) cnt++;
}
}
//输出
printf("%d\n", cnt);
return 0;
}