标签:lex iostream ons while roc desc rem lse 方法
1 #include <iostream> 2 using namespace std; 3 int a[20]; 4 bool isprime[38] = { 5 0,1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 6 }; 7 8 void print(int n) 9 { 10 for (int i = 0; i < n; i++) 11 { 12 if (i==0) 13 printf("%d", a[i]); 14 else 15 { 16 printf(" %d", a[i]); 17 } 18 } 19 printf("\n"); 20 } 21 bool ok(int cur) 22 { 23 if (!isprime[a[cur] + a[cur - 1]]) 24 return false; 25 for (int i = 1; i < cur; i++) 26 { 27 if (a[i] == a[cur]) 28 return false; 29 } 30 return true; 31 } 32 void dfs(int n, int cur) 33 { 34 if (n & 1) 35 return; 36 if (cur == n) 37 { 38 if (isprime[1 + a[cur - 1]]) 39 print(n); 40 } 41 else 42 { 43 for (int i = 2; i <= n; i++) 44 { 45 a[cur] = i; 46 if (ok(cur)) 47 dfs(n, cur + 1); 48 } 49 } 50 } 51 int main() 52 { 53 int n,k=0; 54 while (cin >> n) 55 { 56 k++; 57 printf("Case %d:\n", k); 58 a[0] = 1; 59 dfs(n, 1); 60 printf("\n"); 61 } 62 return 0; 63 }
标签:lex iostream ons while roc desc rem lse 方法
原文地址:https://www.cnblogs.com/1625--H/p/9348176.html