#include <stdio.h>
#include <string.h>
#define N 105
int n;
int a[25];
bool visited[25];
bool prime[N];
void isprime()
{
int i,j;
memset(prime,true,sizeof(prime));
for(i=2;i<N;i++)
{
for(j=i+i;j<N;j+=i)
{
prime[j]=false;
}
}
}
void DFS(int v)
{
int i;
if(v==n+1&&prime[a[n]+1])
{
for(i=1;i<=n;i++)
printf("%d%c",a[i],i == n ? '\n':' ');
}
for(i=2;i<=n;i++)
{
if(!visited[i]&&prime[i+a[v-1]])
{
a[v]=i;
visited[i]=true;
DFS(v+1);
visited[i]=false;
}
}
}
int main()
{
int k=0;
isprime();
while(~scanf("%d",&n))
{
k++;a[1]=1;
memset(visited,false,sizeof(visited));
printf("Case %d:\n",k);
DFS(2);
puts("");
}
}
标签:prime,25,int,Prime,DFS,bool,HDU1016,include From: https://blog.51cto.com/u_16146153/6388586