Description
A Ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in both adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Input
N (0 < n <=)
OutputThe 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.
You is to write a program, that completes above process.
Sample Input
68
Sample Output
Case 1:1 4 3 2 5 a 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
#include<cstdio>#include<cstring>#include<algorithm>UsingNamespace Std;IntIs_prime(int n){For(int k=2; K*k<= N; K++)If(n% K==0)Return0;Return1;}int nA[50], ISP[50], Vis[50];voidDfs(int cur){If(cur= = N&& ISP[A[0]+a[n-1]]){For(int I=0; I< n; I++){If(I!=0)Printf(" ");Printf("%d"A[I]);}Printf("\ n");}ElseFor(int I=2; I<= N; I++)If(!vis[I]&& ISP[I+a[cur-1]]){A[cur]= I; Vis[I]=1;Dfs(cur+1); Vis[I]=0;}}IntMain(){int Kase=0;While(scanf("%d",&n)==1&& N>0){If(Kase>0)Printf("\ n");Printf("Case%d:\ n",++kase);For(int I=2; I<= N*2; I+ +) ISP[i] = is_prime memset (Vis0 sizeof (Vis[0 = 1dfs (1 return 0;"
Program C Brute Force solution