标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36338 Accepted Submission(s): 16024
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int a[50],b[21],test[21]; 6 int n; 7 void prime() 8 { 9 int i,j; 10 for(i=2;i<=50;i++) 11 { 12 for(j=2;j<=i/2;j++) 13 if(i%j==0) 14 break; 15 if(j==(i/2+1)) 16 a[i]=1; 17 else 18 a[i]=0; 19 } 20 } 21 void dfs(int s) 22 { 23 int i,j,k; 24 if(s==n+1&&a[b[n]+1]) 25 { 26 for(i=1;i<n;i++) 27 cout<<b[i]<<" "; 28 cout<<b[n]<<endl; 29 } 30 else for(i=2;i<=n;i++) 31 if(a[b[s-1]+i]&&!test[i]) 32 { 33 b[s]=i; 34 test[i]=1; 35 dfs(s+1); 36 test[i]=0; 37 } 38 } 39 int main() 40 { 41 b[1]=1; 42 prime(); 43 int i,j; 44 while(cin>>n&&n) 45 { 46 cout<<"Case "<<j<<":"<<endl; 47 memset(test,0,sizeof(test)); 48 dfs(2); 49 cout<<endl; 50 j++; 51 } 52 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4955547.html