Prime Ring (C-Brute force solution)

Source: Internet
Author: User

1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 intN, a[ -], isp[ -], vis[ -];6 7 int  is(intx)8 {   9      for(inti =2; I*i <= x; i++)  Ten         if(x% i = =0)  One             return 0;  A         return 1;  - } -  the  -  - voidDfsintcur) - { +     if(cur==n&&isp[a[0]+a[n-1]]) -     { +          for(intI=0; i<n;i++) Aprintf"%d", A[i]); atprintf"\ n"); -     } -     Else  for(intI=2; i<=n;i++) -         if(!vis[i]&&isp[i+a[cur-1]]) -         { -A[cur] =i; inVis[i] =1;  -DFS (cur+1);  toVis[i] =0; +         } - } the  * intMain () $ {Panax Notoginseng     //int n; -     intm=0;  the      while(SCANF ("%d", &n)! =EOF) +     { A      for(intI=2; i<=n*2; i++) theIsp[i] = is(i);  +memset (Vis,0,sizeof(VIS));  -a[0] =1; $m++; $printf"Case %d:\n", m); -Dfs1);  -printf"\ n"); the     } -     return 0;Wuyi}
View Code

Prime Circle (Violence) (Purple book 194 pages)

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 <=)

N (0 < n <= 16)

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

Test instructions
Enter a positive integer n to make the 1-n a ring, making the adjacent two integers a prime number. The output starts with an integer of 1 and is arranged counterclockwise. Just can make a ring output once,N (0 < n <=)


Analysis:
Each ring corresponds to an arrangement of 1--n, but the total number of permutations is up to 16! =2*10^13, the normal notation will time out and apply backtracking.
Dfs. Depth-first traversal.

Code:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 intN, a[ -], isp[ -], vis[ -];6 7 int  is(intx)8 {   9      for(inti =2; I*i <= x; i++)  Ten         if(x% i = =0)  One             return 0;  A         return 1;  - } -  the  -  - voidDfsintcur) - { +     if(cur==n&&isp[a[0]+a[n-1]]) -     { +          for(intI=0; i<n;i++) Aprintf"%d", A[i]); atprintf"\ n"); -     } -     Else  for(intI=2; i<=n;i++) -         if(!vis[i]&&isp[i+a[cur-1]]) -         { -A[cur] =i; inVis[i] =1;  -DFS (cur+1);  toVis[i] =0; +         } - } the  * intMain () $ {Panax Notoginseng     //int n; -     intm=0;  the      while(SCANF ("%d", &n)! =EOF) +     { A      for(intI=2; i<=n*2; i++) theIsp[i] = is(i);  +memset (Vis,0,sizeof(VIS));  -a[0] =1; $m++; $printf"Case %d:\n", m); -Dfs1);  -printf"\ n"); the     } -     return 0;Wuyi}
View Code

Prime Ring (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.