You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// https://www.luogu.com.cn/problem/P2010
#include <bits/stdc++.h>
using namespace std;
int months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//是不是回文字符串
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[18]; //不太会使用这个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)); //截取字符串,字符串转数字
int y2 = stoi(ed.substr(0, 4));
//枚举所有可能年份
for (int i = y1; i <= y2; i++)
// 枚举月份
for (int j = 1; j <= 12; j++)
//构建所有可能的日期
for (int k = 1; k <= 31; k++) {
//闰年的话,二月份的日期不太一样
int maxd = months[j - 1];
if (k > maxd) break; //越界的日期没有意义
//合法日期,构建成字符串
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;
}