Topic 1016: Prime Ring Problem

Source: Internet
Author: User

Problem DescriptionA ring is compose of n circles as shown in digoal. put natural number 1, 2 ,..., n into each circle separately, and the sum of numbers in two adjacent circles shoshould be a prime. note: the number of first circle shoshould always be 1. inputn (0 <n <20 ). outputThe output format is shown as sample below. each row represents a series of circle numbers in the ring beginning from 1 clo Ckwisely and anticlockwisely. the order of numbers must satisfy the above requirements. print solutions in lexicographical order. you are to write a program that completes abve process. print a blank line after each case. sample Input68 Sample OutputCase 1:1 4 3 2 5 61 6 5 2 3 4 Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 3 21 6 7 4 3 8 5 2 SourceAsia 1996, shanghai (Mainland China) Recommen DJGShining [cpp]/********************************* date: * Author: SJF0115 * question: HDU question 1016: Prime Ring Problem * Source: http://acm.hdu.edu.cn/showproblem.php? Pid = 1016 * result: AC * Source: Asia 1996, Shanghai (Mainland China) * Summary: * *********************************/# include <stdio. h> # include <string. h> int prime [] = {, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}; int visited [21]; int value [21]; int n; void DFS (int step) {int I; // The last number also needs to be added to 1 to determine if it is a prime number. if (step = n + 1) {if (prime [value [step-1] + 1]) {// output prime ring for (I = 1; I <= n; I ++) {printf ("% d % c", value [I], I = n? '\ N': '') ;}} else {for (I = 2; I <= n; I ++) {// This data has not been used and is added to the previous number as the prime number if (! Visited [I] & prime [I + value [step-1]) {// mark accessed visited [I] = 1; value [step] = I; DFS (step + 1); visited [I] = 0 ;}}} int main () {int caseNum = 1; while (scanf ("% d ", & n )! = EOF) {memset (visited, 0, n); printf ("Case % d: \ n", caseNum ); // The first number of prime ring is always 1 value [1] = 1; DFS (2); printf ("\ n"); caseNum ++;} return 0 ;} /********************************** Date: * Author: SJF0115 * question: HDU question 1016: Prime Ring Problem * Source: http://acm.hdu.edu.cn/showproblem.php? Pid = 1016 * result: AC * Source: Asia 1996, Shanghai (Mainland China) * Summary: * *********************************/# include <stdio. h> # include <string. h> int prime [] = {, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}; int visited [21]; int value [21]; int n; void DFS (int step) {int I; // The last number also needs to be added to 1 to determine if it is a prime number. if (step = n + 1) {if (prime [value [step-1] + 1]) {// output prime ring for (I = 1; I <= n; I ++) {print F ("% d % c", value [I], I = n? '\ N': '') ;}} else {for (I = 2; I <= n; I ++) {// This data has not been used and is added to the previous number as the prime number if (! Visited [I] & prime [I + value [step-1]) {// mark accessed visited [I] = 1; value [step] = I; DFS (step + 1); visited [I] = 0 ;}}} int main () {int caseNum = 1; while (scanf ("% d ", & n )! = EOF) {memset (visited, 0, n); printf ("Case % d: \ n", caseNum ); // The first number of prime ring is always 1 value [1] = 1; DFS (2); printf ("\ n"); caseNum ++;} return 0 ;}

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.