标签:hdu 1016
# include<stdio.h> # include<algorithm> # include<string.h> using namespace std; int vis[25]; int num[25]; int n,cot; int prime(int sum) { int i; if(sum==2) return false; for(i=2;i*i<=sum;i++) { if(sum%i==0) break; } if(i*i>sum) return true; return false; } void dfs() { int i; for(i=1;i<=n&&cot<=n;i++) { if(!vis[i]&&prime(num[cot-1]+i)) { vis[i]=1; num[cot++]=i; dfs(); vis[num[cot-1]]=0; cot--; } } if(cot==n+1&&prime(num[1]+num[cot-1])) { for(i=1;i<=n;i++) { if(i==1) printf("%d",num[i]); else printf(" %d",num[i]); } printf("\n"); } } int main () { int cas=0; while(~scanf("%d",&n)) { printf("Case %d:\n",++cas); memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); cot=2; vis[1]=1; num[1]=1; dfs(); printf("\n"); } return 0; }
标签:hdu 1016
原文地址:http://blog.csdn.net/lp_opai/article/details/38064401