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

    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). 
    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. 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
    DFS 素数环
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 using namespace std;
    16 
    17 int a[25];
    18 int TM[25];
    19 int n;
    20 
    21 int QQQ(int x)
    22 {
    23     for(int i=2;i<=x/2;i++)
    24     {
    25         if(x%i==0)
    26         return 0;
    27     }
    28     return 1;
    29 }
    30 
    31 void dfs(int k,int CCC)
    32 {
    33     if(k==n)
    34     {
    35             if(QQQ(CCC+1))
    36             {
    37                 a[k]=CCC;
    38                 int flag=1;
    39                 for(int i=0;i<n;i++)
    40                 {
    41                     if(flag)
    42                     {
    43                         flag=0;
    44                         cout<<a[i];
    45                         continue;
    46                     }
    47                     cout<<' '<<a[i];
    48                 }
    49                 cout<<endl;
    50             }
    51             return;
    52     }
    53     for(int i=2;i<=n;i++)
    54     {
    55             if(!TM[i]&&QQQ(CCC+i))
    56             {
    57             TM[i]=1;
    58             a[k]=i;
    59             dfs(k+1,i);
    60             TM[i]=0;
    61             }
    62     }
    63 }
    64 
    65 int main()
    66 {
    67     int add=0;
    68     while(cin>>n)
    69     {
    70         cout<<"Case "<<++add<<":"<<endl;
    71     memset(TM,0,sizeof(TM));
    72     a[0]=1;
    73     dfs(1,1);
    74     cout<<endl;
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    【已解决】github中git push origin master出错:error: failed to push some refs to
    好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
    THINKPHP 5.0目录结构
    thinkphp5.0入口文件
    thinkphp5.0 生命周期
    thinkphp5.0 架构
    Django template
    Django queryset
    Django model
    Python unittest
  • 原文地址:https://www.cnblogs.com/dulute/p/7272731.html
Copyright © 2011-2022 走看看