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
Miguel A. Revilla
1999-01-11Test instructionsEnter a positive integer n, the 1,2,3,...,n is composed of a ring, so that the sum of the adjacent two positive numbers are prime, the output at the beginning of the integer 1 counter-clockwise, the same ring exactly output once.Ideas:Use DFS for backtracking.Code:
#include <cstdio> #include <algorithm>using namespace Std;int s[20];int main () { int n,casex=1; Long long ans,a; while (scanf ("%d", &n)!=eof) { ans=0; for (int i=1;i<=n;i++) scanf ("%d", &s[i]); for (int i=1;i<=n;i++) {for (int j=i;j<=n;j++) { a=1; for (int k=i;k<=j;k++) { a*=s[k]; } Ans=max (ans,a); } } printf ("Case #%d:the maximum product is%lld.\n\n", Casex,ans); casex++; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 524 (Prime ring)