题目地址:http://jobdu.sinaapp.com/problem.php?cid=1040&pid=88
C语言源码:
#include<stdio.h>#include<math.h>int s[18];int mark[18];int pri(int n){ int i,b; b=(int)sqrt(n)+1; if(n==2) return 1; i=2; while(i<=b) { if(n%i==0) return 0; else i++; } return 1;}void input(int i,int n){ int j,flag; if(i==n+1) { flag=0; for(j=1;j<n;j++) { if(pri(s[j]+s[j+1])==0) { flag=1; break; } } if(pri(s[1]+s[n])==0) flag=1; if(flag==0) { for(j=1;j<n;j++) printf("%d ",s[j]); printf("%d\n",s[j]); } } else { for(j=1;j<=n;j++) { if(mark[j]==-1&&pri(s[i-1]+j)==1) { s[i]=j; mark[j]=1; input(i+1,n); mark[j]=-1; } } }}int main(){ int n,num,i; num=1; s[1]=1; while(scanf("%d",&n)!=EOF) { for(i=0;i<=n;i++) mark[i]=-1; mark[1]=1; printf("Case %d:\n",num++); input(2,n); printf("\n"); }}