题目链接:特殊的质数肋骨
通过观察可以看出第一个数一定是 2 3 5 7 中的一个(必须满足第一个数是质数。)
然后我们再枚举满足的奇数(偶数肯定会出现合数!)
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int n;
int pd(int x)
{
for(int i=2;i*i<=x;i++)
if(x % i == 0)
return true;
return false;
}
void dfs(int now,int ans) // now 记录当前是第几位 ans 记录前 now 位的值
{
if(pd(ans)) return ;
if(now == n)
{
printf("%d\n",ans);
return ;
}
for(int i=1;i<=9;i+=2)//只找奇数
{
dfs(now+1,ans*10+i);
}
}
int main()
{
scanf("%d",&n);
dfs(1,2);
dfs(1,3);
dfs(1,5);
dfs(1,7);
return 0;
}