今天看啥
热点:

HDU_1016,惠普g41016tx


#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;

const int Max = 40;

bool prime[Max];
bool vis[Max];
int A[Max];
int n;

void IsPrime() {
    prime[0] = prime[1] = 0; prime[2] = 1;
    for(int i = 3; i < Max; i++)
        prime[i] = (i%2 == 0) ? 0 : 1;
    int t = (int)sqrt(Max*10);
    for(int i = 3; i <= t; i++)
        if(prime[i])
            for(int j = i*i; j < Max; j += 2*i)
                prime[j] = 0;
}

void dfs(int cur) {
    if(cur == n && prime[A[0]+A[n-1]]) {
        for(int i = 0; i < n; i++)   //打印的时候是从A[0]开始打印的
            {printf("%d", A[i]);if(i != n-1) printf(" ");}
            printf("\n");
    }
    else for(int i = 2; i <= n; i++)
    if(!vis[i] && prime[i+A[cur-1]]) {
  //  如果这个数还没有使用过并且和前一个数之和为素数的话
        A[cur] = i;
        vis[i] = 1;
        dfs(cur+1);
        vis[i] = 0;   //清除标志
    }
}

int main(){
    int cnt = 0;
    IsPrime();
    while(scanf("%d", &n) != EOF) {
            memset(vis, 0, sizeof(vis));
        printf("Case %d:\n", ++cnt);
        A[0] = 1;
        dfs(1);
        printf("\n");
    }
    return 0;
}

www.bkjia.comtruehttp://www.bkjia.com/ASPjc/998115.htmlTechArticleHDU_1016,惠普g41016tx #includecstdio#includecmath#includeiostream#includecstringusing namespace std;const int Max = 40;bool prime[Max];bool vis[Max];int A[Max];int n;void IsPri...

相关文章

    暂无相关文章
相关搜索: HDU_1016

帮客评论

视觉看点