Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34799 Accepted Submission(s): 15411
Problem Description
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.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order. 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
Source
Recommend
JGShining | We have carefully selected several similar problems for you:
这是我第一道接触的搜索题,当时参考学长的代码写,却始终不能理解代码的意思,于是就一遍一遍第敲,直到整个代码都背下来了,后来接触的搜索逐渐增加,终于理解了这道题的代码,每次看到这道题,就能想起当年苦逼的敲代码。。。
题意:输入一个数n,代表从1到n个数,排序排成一个环,使得相邻两个数之和为素数(第一个和最后一个数也要满足),如果有多组情况,必须按从小到大的顺序输出,
附上代码:
1 #include2 using namespace std; 3 int a[30]= { 0,1},b[30],sushu[50],n; //a表示素数环中的数字,b用来标记,sushu标记是否是素数 4 void DFS(int s,int k) //s代表当前循环到第几个数,k标记此时是否是第一次进人深搜函数 5 { 6 int i,j; 7 if(s>n) 8 { 9 if(sushu[a[1]+a[n]]) //最后一个数字和第一个数字的和10 {11 for(i=1; i<=n; i++) //全部满足,输出这组数据的全部数12 {13 if(i>1) cout<<" ";14 cout< >n)45 {46 cout<<"Case "< <<":"<