社区讨论
呜呜,全WA,怎么办呀
P5440【XR-2】奇迹参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lo2x1fij
- 此快照首次捕获于
- 2023/10/23 21:10 2 年前
- 此快照最后确认于
- 2023/10/23 21:10 2 年前
CPP
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int n, res;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int to_int(string date) {
int res = 0;
for (int i = 0 ; i < date.size() ; ++i) {
res = res * 10 + date[i] - '0';
}
return res;
}
bool is_prime(string date) {
int x = to_int(date.substr(6, 2));
for (int i = 2 ; i <= x / i ; ++ i) {
if (x % i == 0) return false;
}
x = to_int(date.substr(4, 4));
for (int i = 2 ; i <= x / i ; ++ i) {
if (x % i == 0) return false;
}
x = to_int(date);
for (int i = 2 ; i <= x / i ; ++ i) {
if (x % i == 0) return false;
}
return true;
}
bool run_year(int x) {
if ( x % 400 == 0 && x % 100 != 0 || x % 4 == 0 ) return true;
return false;
}
bool check_valid(int year, int month, int day) {
if (month == 0 || month > 12) return false;
if (day == 0) return false;
if (month != 2) {
if (day > days[month]) return false;
} else {
int leap = year % 400 == 0 && year % 100 != 0 || year % 4 == 0;
if (day > 28 + leap) return false;
}
return true;
}
bool judge(int x)
{
if(x < 1 || x > 12) return false;
}
bool judge_1(int x)
{
if(x > 3) return false;
}
void dfs(string date, int idx) {
//已经是一个完整的数了,判断输出答案
if(idx > 5 && judge(to_int(date.substr(4 , 2)))) return ;
if(idx > 6 && judge_1(to_int(date.substr(6 , 1)))) return ;
if (idx >= 8) {
int year = to_int(date.substr(0, 4)), month = to_int(date.substr(4, 2)), day = to_int(date.substr(6, 2));
if (check_valid(year, month, day) && is_prime(date)) res++;
return ;
} else if (date[idx] == '-') {
for (int i = 0 ; i <= 9 ; ++i) {
string p = date;
p[idx] = i + '0';
dfs(p, idx + 1);
}
} else {
//当前位数已经确定,直接搜索下一位
dfs(date, idx + 1);
}
}
int main() {
cin >> n;
while (n--) {
string date;
cin >> date;
res = 0;
dfs(date, 0);
cout << res << endl;
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...