zoukankan      html  css  js  c++  java
  • Prime Ring Problem

    Prime Ring Problem

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

    Note: the number of first circle should always be 1.


    Inputn

    (0 < n < 20).
    Output

      The 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. Print solutions in lexicographical order. 

    You are to write a program that completes above process.

    Print a blank line after each case.
    Sample Input

    6
    8

    Sample Output

    Case 1:
    1 4 3 2 5 6
    1 6 5 2 3 4
    
    Case 2:
    1 2 3 8 5 6 7 4
    1 2 5 8 3 4 7 6
    1 4 7 6 5 8 3 2
    1 6 7 4 3 8 5 2

     
    
    
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #define maxn 20
     5 bool mark[maxn+5];
     6 int key[maxn+5];
     7 bool dp[maxn][maxn];
     8 int n;
     9 void is_prime()
    10 {
    11     //memset(dp,true,sizeof(dp));
    12     for(int i = 1;i < 20; i++)
    13         for(int j = 1;j < 20;j++)
    14         {
    15             int cmp = i + j;
    16             int flag = 0;
    17             for(int k = 2; k <= sqrt(cmp); k++)
    18             {
    19                 if(cmp % k == 0)
    20                 {
    21                     dp[i][j] = false;
    22                     flag = 1;
    23                     break;
    24                 }
    25             }
    26             if(!flag)
    27                 dp[i][j] = true;
    28         }
    29 }
    30 
    31 void print()
    32 {
    33     for(int i = 1;i <= n; i++)
    34     {
    35         if(i != 1)
    36             printf(" ");
    37         printf("%d",key[i]);
    38     }
    39     printf("
    ");
    40 }
    41 
    42 void dfs(int ans)
    43 {
    44     if(ans == n && dp[key[ans]][1])
    45     {
    46         print();
    47     }
    48     for(int i = 2; i <= n; i++)
    49     {
    50         if(mark[i] == false)
    51         {
    52             if(dp[i][key[ans]])
    53             {
    54                 mark[i] = true;
    55                 key[ans+1] = i;
    56                 dfs(ans+1);
    57                 mark[i] = false;
    58             }
    59         }
    60     }
    61 }
    62 
    63 int main()
    64 {
    65     int count = 0;
    66     memset(dp,false,sizeof(dp));
    67     is_prime();
    68     while(scanf("%d",&n) != EOF)
    69     {
    70         printf("Case %d:
    ",++count);
    71         memset(mark,false,sizeof(mark));
    72         key[1] = 1;
    73         dfs(1);
    74         printf("
    ");
    75     }
    76     return 0;
    77 }
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #define maxn 20
     5 bool mark[maxn+5];
     6 int key[maxn+5];
     7 bool dp[maxn][maxn];
     8 int n;
     9 void is_prime()
    10 {
    11     memset(dp,true,sizeof(dp));
    12     for(int i = 1;i < 20; i++)
    13         for(int j = 1;j < 20;j++)
    14         {
    15             int cmp = i + j;
    16             for(int k = 2; k <= sqrt(cmp); k++)
    17             {
    18                 if(cmp % k == 0)
    19                 {
    20                     dp[i][j] = false;
    21                     break;
    22                 }
    23             }
    24         }
    25 }
    26 
    27 void print()
    28 {
    29     for(int i = 1;i <= n; i++)
    30     {
    31         if(i != 1)
    32             printf(" ");
    33         printf("%d",key[i]);
    34     }
    35     printf("
    ");
    36 }
    37 
    38 void dfs(int ans)
    39 {
    40     if(ans == n && dp[key[ans]][1])
    41     {
    42         print();
    43     }
    44     for(int i = 2; i <= n; i++)
    45     {
    46         if(mark[i] == false)
    47         {
    48             if(dp[i][key[ans]])
    49             {
    50                 mark[i] = true;
    51                 key[ans+1] = i;
    52                 dfs(ans+1);
    53                 mark[i] = false;
    54             }
    55         }
    56     }
    57 }
    58 
    59 int main()
    60 {
    61     int count = 0;
    62     is_prime();
    63     while(scanf("%d",&n) != EOF)
    64     {
    65         printf("Case %d:
    ",++count);
    66         key[1] = 1;
    67         dfs(1);
    68         printf("
    ");
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    Promise
    replace
    mongogogog
    sh back mongo
    mongodb 备份 还原 导出 导入
    mongo virtual
    openssl
    isMobile
    google api autocomplete
    npm
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6590405.html
Copyright © 2011-2022 走看看