题解:B4158 [BCSP-X 2024 12 月小学高年级组] 质数补全
2789617221guo · · 题解
思路
这题是一道简单的 DFS 搜索题,在读入字符串 *,则计数器
这里可以进行一个特判:若
否则就调用函数 dfs(int t,string s),其中 DFS 函数直接return;其他情况下,若当前枚举的数位 dfs 递归,否则就从 0 到 9 枚举数字。
最后,不要忘了在每个数据组开始时清空所有变量。
代码(我知道你们在等的是这个)
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const int N = 10;
string str;
int p, pos[N];
bool flag = 0;
bool prime(string s) {
int a = atoi(s.c_str());
if (a <= 1)
return 0;
for (int i = 2; i * i <= a; i++) {
if (a % i == 0)
return 0;
}
return 1;
}
void dfs(int t, string s) {
if (flag)
return;
if (t > p) {
if (prime(s)) {
cout << s << endl;
flag = 1;
}
return;
}
int c = pos[t];
if (c == 0) {
for (int i = 1; i <= 9; i++) {
s[c] = i + '0';
dfs(t + 1, s);
s[c] = '*';
}
} else {
for (int i = 0; i <= 9; i++) {
s[c] = i + '0';
dfs(t + 1, s);
s[c] = '*';
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
cin >> str;
p = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '*') {
pos[++p] = i;
}
}
flag = 0;
if (p == 0) {
if (prime(str))
cout << str << endl;
else {
cout << -1 << endl;
}
} else {
dfs(1, str);
if (!flag)
cout << -1 << endl;
}
}
return 0;
}
Upd 25.3.4 AC记录
AC。
管理大大求过,我已经交了5次了QWQ