标签:
1 #include <stdio.h> 2 #include <string.h> 3 int n; 4 int vis[30], a[30]; 5 int judge(int s) 6 { 7 int flag = 1; 8 for(int i = 2; i <= s/2; i++) 9 { 10 if(s%i == 0) 11 { 12 flag = 0; 13 break; 14 } 15 } 16 return flag; 17 } 18 void dfs(int s, int cnt) 19 { 20 int i, j; 21 if(cnt == n+1 && judge(a[1]+a[n])) 22 { 23 for(j = 1; j < n+1; j++) 24 { 25 if(j != 1) 26 printf(" "); 27 printf("%d", a[j]); 28 } 29 printf("\n"); 30 return ; 31 } 32 for(i = 1; i <= n; i++) 33 { 34 if(judge(i + s) && !vis[i]) 35 { 36 a[cnt] = i; 37 vis[i] = 1; 38 dfs(i, cnt + 1); 39 vis[i] = 0; 40 41 } 42 } 43 return ; 44 } 45 int main() 46 { 47 int num = 1; 48 while(~scanf("%d", &n)) 49 { 50 printf("Case %d:\n", num++); 51 memset(vis, 0, sizeof(vis)); 52 vis[1] = 1; 53 a[1] = 1; 54 dfs(1, 2); 55 printf("\n"); 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/digulove/p/4740447.html