ac代码:

#include<iostream> using namespace std; int map[61]; int use[21]; int ss[22]; int n; void dfs(int i) { if(i==n&&map[ss[i-1]+1]==0) { for(int l=0;l<n-1;l++) cout<<ss[l]<<" "; cout<<ss[n-1]<<endl; } else { for(int j=2;j<=n;j++) { if(!use[j]&&map[ss[i-1]+j]==0) { use[j]=1; ss[i]=j; i++; dfs(i); i--; use[j]=0; } } } } int main() { memset(map,0,sizeof(map)); int i,j; map[0]=map[1]=1; for(i=2;i<=30;i++) { for(j=i*i;j<60;j+=i) { map[j]=1; } } int num=1; while(cin>>n) { printf("Case %d:\n",num++); memset(use,0,sizeof(use)); ss[0]=1; dfs(1); cout<<endl; } return 0; }