HDU_1016,惠普g41016tx

來源:互聯網
上載者:User

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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.