Program C Brute Force solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.