hdu1016

来源:互联网 发布:手机音乐制作软件 编辑:程序博客网 时间:2024/05/17 03:57

特判奇偶性

#include <cstdio>#include <iostream>#include <cstring>using namespace std;int const MAXN = 60;int num[MAXN],a[MAXN],vis[MAXN],prime[MAXN];int n;void Get_Prime(){    memset(prime,0,sizeof(prime));    for(int i = 2;i <= 50;i++){        if(prime[i]) continue;        for(int j = i + i;j <= 50;j += i){            prime[j] = 1;        }    }}void Dfs(int x){    if(x == n){        if(!prime[a[0] + a[n - 1]]){            printf("%d",a[0]);            for(int i = 1;i < n;i++){                printf(" %d",a[i]);            }            printf("\n");        }        return ;    }    for(int i = 2;i <= n;i++){        int t = i + a[x - 1];        if(prime[t]) continue;        if(!vis[i]  ){            vis[i] = 1;            a[x] = i;            Dfs(x + 1);            vis[i] = 0;        }    }}int main(){    Get_Prime();    int k = 1;    while(~scanf("%d",&n)){        if(n <= 0 || n >= 20) break;        printf("Case %d:\n",k++);        if(n & 1){            printf("\n");            continue;        }        memset(vis,0,sizeof(vis));        a[0] = 1;        Dfs(1);        printf("\n");    }    return 0;}


0 0