标签:
#include<stdio.h> #include<string.h>
int prime[40]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,01,1,1,0,1,1};
int visit[21];
int ring[21];
/* void Is_prime() {
int i,j;
prime[0]=prime[1]=1;
for(i=2;i<=6;++i)
for(j=i*i;j<40;j+=i)
prime[j]=1; }
*/
void DFS(int k,int n) {
int i;
if(k==n+1&&prime[ring[n]+ring[1]]==0)
{
printf("1");
for(i=2;i<=n;++i)
printf(" %d",ring[i]);
printf("\n");
return;
}
for(i=2;i<=n;++i) {
if(!visit[i]&&!prime[i+ring[k-1]]) {
visit[i]=1;
ring[k]=i;
DFS(k+1,n);
visit[i]=0;
}
}
}
int main() {
int T,n;
T=1; // Is_prime();
while(scanf("%d",&n),n) {
printf("Case %d:\n",T++);
if(n==1) {
printf("1\n");
continue;
}
if(n&1) {
printf("No Answer\n");
continue;
}
memset(visit,0,sizeof(visit));
visit[1]=ring[1]=1;
DFS(2,n);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/CAOYR/p/5471053.html