Huas Summer trainning #3 C

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

The main idea: make the 1~n these numbers form a ring, and 22 add if the prime number, the output of all solutions.

Problem-solving ideas: The first number is always 1, directly from the second number of Dfs to traverse, traverse the bottom of the department is not satisfied, back to the previous node, know to traverse all feasible solutions.

Code:

1#include <iostream>2#include <cstring>3 using namespacestd;4 Const intmaxn= -+5;5 inta[maxn],b[maxn],d[maxn],n,i,j;6 intSushu (intx)7 {8      for(i=2; i*i<=x;i++)9         if(x%i==0)Ten             return 0; One     return 1; A } - voidDfsintcur) - { the     if(cur==n&&d[a[0]+a[cur-1]]) -     { -cout<<a[0]; -          for(intI=1; i<n;i++) +cout<<" "<<A[i]; -cout<<Endl; +     } A     Else at     { -          for(intI=2; i<=n;i++) -         { -             if(!b[i]&&d[i+a[cur-1]]) -             { -a[cur]=i; inb[i]=1; -DFS (cur+1); tob[i]=0; +             } -         } the     } * } $ intMain ()Panax Notoginseng { -     intp=0, t=0; the      while(cin>>n&&N) +     { A         if(p++) thecout<<Endl;  +cout<<" Case"<<++t<<":"<<Endl; -      $Memset (A,0,sizeof(a)); $memset (D,0,sizeof(d)); -        for(j=2; j<n*2; j + +) -d[j]=Sushu (j); thememset (b,0,sizeof(b)); -a[0]=1;WuyiDfs1); the     } -     return 0; Wu}

Huas Summer trainning #3 C

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.