最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

機試小課堂丨搜索周·例題講解③《Prime Path》

2021-01-26 18:47 作者:蘇世考研  | 我要投稿


蘇世計算機考研,程序猿專屬的學習分享社區(qū)


【聲明:本文為原創(chuàng)文章,未經(jīng)同意,嚴禁轉(zhuǎn)載和抄襲,違者將追究其法律責任】


蘇世機試小課堂,考研機試不再慌!


公主號:蘇世學社考研? 蘇世計算機考研


Prime Ring Problem


Time Limit:4000/2000MS(Java/Others)

Memory Limit:65535/32768K(Java/Others)


Description


A ring iscompose of n circles as shown in diagram. Put natural number 1, 2, ..., n intoeach circle separately, and the sum of numbers in two adjacent circles shouldbe a prime.


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



Input


n (0 < n< 20).


Output


The outputformat is shown as sample below. Each row represents a series of circle numbersin the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographicalorder.


You are to write a program that completes above process.


Print a blank line after each case.


Sample Input


? ? 6

? ? 8


Sample Output


? ??Case 1:

? ? 1 4 3 2 5 6

? ? 1 6 5 2 3 4


? ? Case 2:

? ? 1 2 3 8 5 6 7 4

? ? 1 2 5 8 3 4 7 6

? ? 1 4 7 6 5 8 3 2

? ? 1 6 7 4 3 8 5 2



答案


①讀題:


給出一個n,對1到n這n個數(shù)排序,構(gòu)造一個環(huán),使得相鄰兩數(shù)之和為素數(shù),這個環(huán)叫做素數(shù)環(huán)。


②想出思路:


用深度優(yōu)先搜索(DFS)遍歷每一種情況,依次輸出即可。


③動手編程:


④測試樣例:


⑤提交代碼:


進入下面的鏈接提交代碼:


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


⑥返回評測結(jié)果:

至此,這道題我們就已經(jīng)完成了。



本題總結(jié)


經(jīng)典題型素數(shù)環(huán),從1出發(fā),一層一層推下去判斷是否符合條件,然后回溯記錄路徑即可。


未完待續(xù)

蘇世學社旗下品牌,專注于計算機考研

計算機考研一手資訊,原創(chuàng)高質(zhì)量干貨

深度的學習分享丨咨詢前輩丨個性化指導



機試小課堂丨搜索周·例題講解③《Prime Path》的評論 (共 條)

使用qq登录你需要登录后才可以评论。
兴业县| 香港| 中山市| 龙井市| 延川县| 沐川县| 紫云| 沙洋县| 赣州市| 鄂尔多斯市| 乐山市| 龙海市| 普兰店市| 萝北县| 鹤岗市| 博兴县| 轮台县| 镇雄县| 大化| 甘肃省| 安国市| 周至县| 桐庐县| 湘乡市| 泽库县| 瑞金市| 桦甸市| 黄冈市| 五家渠市| 仁布县| 东城区| 昌平区| 疏附县| 陈巴尔虎旗| 海口市| 神池县| 南宁市| 田阳县| 房产| 平邑县| 错那县|