日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[HDOJ1016]Prime Ring Problem

發(fā)布時間:2024/10/12 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [HDOJ1016]Prime Ring Problem 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1016

原題:

A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be 1.

輸入N個數(shù)字,求1到N這N個數(shù)字連成環(huán)以后兩兩相加均為素數(shù)的所有情況。

非常經(jīng)典的DFS題。

?

?

#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int n; int cir[21]; int prime[41]; int vis[21]; int isPrime(int x) {int i;int sqx = sqrt((float)x);for(i = 2; i <= sqx; i++){if(x % i == 0){????return 0;}}return 1; }void dfs(int x) {int i;if(x == n && prime[cir[1]+cir[n]])????//make a circle ????{for(i = 1; i < n; i++){printf("%d ", cir[i]);}printf("%d\n", cir[n]);return ;}for(i = 2; i <= n; i++){if(vis[i] == 0 && prime[cir[x]+i]){cir[x+1] = i;vis[i] = 1;dfs(x+1);vis[i] = 0;????//reset ????????}} }int main() {int i, Case = 1;for(i = 1; i < 41; i++)????//list primes. ????{if(isPrime(i)){prime[i] = 1;}}while(scanf("%d", &n) != EOF && n){printf("Case %d:\n", Case++);memset(vis, 0, sizeof(vis));cir[1] = 1;vis[1] = 1;dfs(1);printf("\n");}return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/kirai/p/4515941.html

總結(jié)

以上是生活随笔為你收集整理的[HDOJ1016]Prime Ring Problem的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。