时间暴力问题转换成序列暴力问题
分别生成年份,日月,时间的 4 个数字序列,再分别判断是否合法
#include<bits/stdc++.h>
using namespace std;
int day[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 判断是否闰年
bool is_run(int y) {
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) return true;
return false;
}
// 获得数字序列
vector<string> get(char a, char c) {
vector<string> tot;
// cout << a << " " << c << endl;
// string aa = to_string(a), cc = to_string(c);
string aa, cc;
aa += a;
cc += c;
// cout << aa << " " << cc << endl;
tot.push_back(aa + cc + cc + cc);
tot.push_back(cc + aa + cc + cc);
tot.push_back(cc + cc + aa + cc);
tot.push_back(cc + cc + cc + aa);
return tot;
}
// 年份必定合法,不用判断
// 判断日月是否合法
bool check_m(string s1, string s) {
int m = (s[0] - '0') * 10 + s[1] - '0';
int d = (s[2] - '0') * 10 + s[3] - '0';
// cout << m << " " << d << endl;
if (m > 12 || m <= 0) return false;
int y = (s1[0] - '0') * 1000 + (s1[1] - '0') * 100 + (s1[2] - '0') * 10 + s1[3] - '0';
if (is_run(y)) {
day[2] = 29;
}else day[2] = 28;
if (d <= 0 || d > day[m]) return false;
return true;
}
// 判断时间是否合法
bool check_s(string s) {
int h = (s[0] - '0') * 10 + s[1] - '0';
int f = (s[2] - '0') * 10 + s[3] - '0';
if (h < 0 || h > 24) return false;
if (f < 0 || f > 60) return false;
return true;
}
int main() {
int ans = 0;
// vector<string> t = get('1', '2');
// for (string s : t) cout << s << endl;
for (char i = '0'; i <= '9'; ++ i) {
for (char j = '0'; j <= '9'; ++ j) {
if (i == j) continue;
vector<string> tot = get(i, j);
// for (string s : tot) cout << s << endl;
for (int y = 0; y < tot.size(); ++ y) {
for (int m = 0; m < tot.size(); ++ m) {
for (int d = 0; d < tot.size(); ++ d) {
if (check_m(tot[y], tot[m]) && check_s(tot[d])) ++ ans;
}
}
}
}
}
cout << ans << endl;
return 0;
}