hdu acm 1016
那个 i ,我开始的时候定义成全局变量了,后来一直错,哈哈哈哈哈哈。
#include <stdio.h> #include <string.h> int prime[40] = {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,0,0}; int visited[21], a[21], n, num; void dfs(int num) { if(num == n && prime[a[num-1] + a[0]]) { for(int i=0; i<num-1; i++) printf("%d ", a[i]); printf("%d\n", a[num-1]); } else { for(int i=2; i<=n; i++) { if(!visited[i]) { if(prime[i + a[num-1]]) { visited[i] = 1; a[num] = i; dfs(num + 1); visited[i] = 0; } } } } } int main() { num = 0; while(~scanf("%d", &n)) { num ++; printf("Case %d:\n", num); memset(visited, 0, sizeof(visited)); a[0] = 1; dfs(1); printf("\n"); } return 0; }