Topic website : http://codevs.cn/problem/1031/
Title Description Description
A prime ring of size n (n<=17) is a number ring consisting of 1 to N of a total of n natural numbers, and the sum of each of the two contiguous numbers on a number of rings is prime. As a prime-number ring of size 6. For convenience of description, the first digit on the number ring is always 1. If available 1 4 3 2 5 6来 description. If the two prime number rings, the numbers are arranged in the same order as essentially the same. Now ask you to find all the different rings of the essence.
Enter a description input Description
There is only one number n, which represents the size of the required prime ring. Such as:
Outputs description Output Description
Each line describes a number loop, and if there are multiple sets of solutions, the output is from small to large in dictionary order. Such as:
sample input to sample
6
sample output Sample outputs
1 4 3 2 5 6
1 6 5 2 3 4
Data Size & Hint
N<=17
puzzle: Brute Force search
The code is detailed :
#include <cstdio>#include <cmath>using namespace STD;inta[ -],n,b[ -];//a is used to store optional data and B is used to store the data that has been placedintFintN//test is a prime number, 1 is not, 0 is a prime number{ for(intI=2; i<=sqrt(n); i++) {if(n%i==0)return 0; }return 1;}voidDfsintX//Search{if(x==n) {if(F (b[x-1]+1)){//See if the last and first can be added to a prime number for(intI=0; i<n;i++) {printf("%d", B[i]); }printf("\ n"); } }Else{intU for(intI=1; i<n;i++)//Traverse an optional number{if(a[i]!=0&&f (a[i]+b[x-1]) {u=a[i]; B[x]=u; a[i]=0; DFS (x+1); b[x]=0; A[i]=u; } }if(b[x]==0)return; }}intMain () {scanf("%d", &n);if(n%2==1)return 0;//For odd number direct no answer (don't ask me why, I just put it all tested again, the specific verification also need tell) for(intI=1; i<n;i++) a[i]=i+1; b[0]=1;//Make sure the first one is number 1thDfs1);//Starting from 1th PS: number No. 0 is already 1.}
1031 Prime number Ring (deep search practice)