题目描述
回文日期 - 蓝桥云课 (lanqiao.cn)
题目分析
对于此题,我们最先想到的是暴力解法,将每一种情况经行循环查找,在查找的过程中记录下答案,回文日期就是字符串判断回文,ABABBABA型回文日期可以将回文经行特判从而确定
#include<bits/stdc++.h>
using namespace std;
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int nx, ans, f1, f2;
string ans1, ans2;
bool is_ren(int n)
{if(n % 400 == 0 || (n % 100 != 0 && n & 4 == 0))return true;return false;
}
bool flag1(string s)
{int n = s.size();for(int i = 0; i < n / 2; i ++){if(s[i] != s[n - i - 1])return false;}ans1 = s;return true;
}
bool flag2(string s)
{int n = s.size();for(int i = 0; i < n / 2; i ++){if(s[i] != s[n - i - 1])return false;}if(s[0] == s[2] && s[2] == s[5] && s[5] == s[7] && s[1] == s[3] && s[3] == s[4] && s[4] == s[6] && s[0] != s[1]){ans2 = s;return true;}return false;
}
int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);cin >> nx;//算出输入的年月日 int y = nx / 10000;int mo = nx / 100 % 100;int d = nx % 100;//从输入的年开始(2020是随意的数字) for(int i = y; i <= 10000; i ++){if(is_ren(i))m[2] = 29;else m[2] = 28;for(int j = 1; j <= 12; j ++){for(int k = 1; k <= m[j]; k ++){if(i == y && j == 1 && k == 1)//如果是刚进入循环的时候,因为此时当天不算故看下一天是哪一天 {if(d + 1 <= m[mo])//下一天不跨月 {j = mo;k = d + 1;}else if(mo + 1 <= 12)//下一天跨月不跨年 {j = mo + 1;k = 1;}else//下一天跨年 {i = y ++;j = 1;k = 1;}}//将这一天转为字符串进行判断 string s = "";s += to_string(i);if(j < 10)s += to_string(0);s += to_string(j);if(k < 10)s += to_string(0);s += to_string (k);if(ans1.size() != 8)flag1(s);if(ans2.size() != 8)flag2(s);if(ans1.size() == 8 && ans2.size() == 8)break;}if(ans1.size() == 8 && ans2.size() == 8)break;}if(ans1.size() == 8 && ans2.size() == 8)break;}cout << ans1 << '\n' << ans2;return 0;
}