现在的位置: 首页 > 综合 > 正文

hdu1016

2018年04月21日 ⁄ 综合 ⁄ 共 688字 ⁄ 字号 评论关闭

特判奇偶性

#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;
}
【上篇】
【下篇】

抱歉!评论已关闭.