无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(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》的評論 (共 條)

分享到微博請遵守國家法律
烟台市| 纳雍县| 合阳县| 措勤县| 龙胜| 威远县| 息烽县| 桂林市| 沙坪坝区| 盐边县| 黄梅县| 井冈山市| 阜阳市| 泰兴市| 承德市| 潞西市| 开平市| 新建县| 平山县| 丰都县| 丰县| 和政县| 成武县| 荣成市| 木里| 怀化市| 沁水县| 田东县| 右玉县| 黄石市| 无极县| 博爱县| 威宁| 南陵县| 雷波县| 高阳县| 大丰市| 盐亭县| 耿马| 丰县| 清徐县|