HDU 1016Prime Ring Problem

來源:互聯網
上載者:User

標籤:bsp   namespace   nbsp   cin   main   ace   color   count   style   

http://acm.hdu.edu.cn/showproblem.php?pid=1016

題意:輸入一個數,給出符合要求的素數環。

經典的dfs遍曆。

 1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 using namespace std; 5  6 int visited[25]; 7 int pos[25]; 8 int n, kase = 0; 9 10 int judge(int x)                 //判斷是否為素數11 {12     for (int i = 2; i <= sqrt(x); i++)13     {14         if (x%i == 0)     return 0;15     }16     return 1;17 }18 19 void dfs(int x,int count)20 {21     pos[count] = x;22     visited[x] = 1;23     if (count == n && judge(x + pos[1]))24     {25         for (int i = 1; i < n; i++)26             cout << pos[i] << " ";27         cout << pos[n];28         cout << endl;29     }30     for (int k = 2; k <= n; k++)31     {32         if (!visited[k] && judge(k+pos[count]))33         {34             dfs(k, count+1);35             visited[k] = 0;36         }37     }38 }39 40 int main()41 {42     while (cin >> n && n)43     {44         memset(visited, 0, sizeof(visited));45         memset(pos, 0, sizeof(pos));46         kase++;47         cout << "Case " << kase << ":" << endl;48         dfs(1,1);49         cout << endl;50     }51     return 0;52 }

 

HDU 1016Prime Ring Problem

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.