首页 > 代码库 > UVA - 524

UVA - 524

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4  5 using namespace std; 6  7 int visit[20]; 8  9 int isPrime(int n) {10     for (int i = 2;i * i <= n;i++) {11         if (n % i == 0) return 0;12     }13     return 1;14 }15 16 int dfs(int* A,int n,int cur) {17     if (cur == n && isPrime(A[0] + A[cur - 1])) {18         for (int i = 0;i < n - 1;i++) {19             cout << A[i] << " ";20         } 21         cout << A[n - 1] << endl;22     }23     else {24         for (int i = 2;i <= n;i++) {25             if (!visit[i] && isPrime(i + A[cur - 1])) {26                 visit[i] = 1;27                 A[cur] = i;28                 dfs(A,n,cur + 1);29                 visit[i] = 0;30             }31         }32     }33 }34 35 int main () {36     int n;37     int pos = 0;38     while (cin >> n) {39         if (pos) cout << endl;40         printf("Case %d:\n",++pos);41         int a[20];42         memset(visit,0,sizeof(visit));43         visit[1] = 1;44         a[0] = 1; 45         dfs(a,n,1);46     }47 }

 

UVA - 524