• hdu 1016Prime Ring Problem


    http://acm.hdu.edu.cn/showproblem.php?pid=1016

    Prime Ring Problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 13648    Accepted Submission(s): 6220


    Problem Description
    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.

     

     

    Input
    n (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
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 int a[22];
     4 int vis[22];
     5 int n;
     6 int prime(int x)
     7 {
     8     int i;
     9     int flag=1;
    10     if(x>=2)
    11     {
    12         if(x==2) return 1;
    13         else
    14         {
    15             for(i=2;i*i<=x;i++)
    16             {
    17                 if(x%i==0)
    18                 {
    19                     flag=0;
    20                     break;
    21                 }
    22             }
    23             if(flag) return 1;
    24             else return 0;
    25         }
    26     }
    27     else return 0;
    28 }
    29 
    30 void dfs(int x)
    31 {
    32     int i;
    33     if(x==n&&prime(a[0]+a[n-1]))
    34     {
    35         printf("1");
    36         for(i=1;i<n;i++)
    37         printf(" %d",a[i]);
    38         printf("\n");
    39     }
    40     else
    41     {
    42         for(i=2;i<=n;i++)
    43         {
    44             if(!vis[i]&&prime(a[x-1]+i))
    45             {
    46                 vis[i]=1;
    47                 a[x]=i;
    48                 dfs(x+1);
    49                 vis[i]=0;
    50             }
    51         }
    52     }
    53     
    54 }
    55 int main()
    56 {
    57     int count=0;
    58     while(~scanf("%d",&n))
    59     {
    60         count++;
    61         memset(vis,0,sizeof(vis));
    62         a[0]=1;
    63         printf("Case %d:\n",count);
    64         dfs(1);
    65         printf("\n");
    66     }
    67 }

    数据小,列举出来:

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 int a[22];
     4 int vis[22];
     5 int n;
     6 int prime[38] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1};
     7 void dfs(int x)
     8 {
     9     int i;
    10     if(x==n&&prime[a[0]+a[n-1]])
    11     {
    12         printf("1");
    13         for(i=1;i<n;i++)
    14         printf(" %d",a[i]);
    15         printf("\n");
    16     }
    17     else
    18     {
    19         for(i=2;i<=n;i++)
    20         {
    21             if(!vis[i]&&prime[a[x-1]+i])
    22             {
    23                 vis[i]=1;
    24                 a[x]=i;
    25                 dfs(x+1);
    26                 vis[i]=0;
    27             }
    28         }
    29     }
    30     
    31 }
    32 int main()
    33 {
    34     int count=0;
    35     while(~scanf("%d",&n))
    36     {
    37         count++;
    38         memset(vis,0,sizeof(vis));
    39         a[0]=1;
    40         printf("Case %d:\n",count);
    41         dfs(1);
    42         printf("\n");
    43     }
    44 }
  • 相关阅读:
    Redis简单案例(二) 网站最近的访问用户
    Redis简单案例(一) 网站搜索的热搜词
    Basic Tutorials of Redis(9) -First Edition RedisHelper
    Basic Tutorials of Redis(8) -Transaction
    Basic Tutorials of Redis(7) -Publish and Subscribe
    Basic Tutorials of Redis(6)
    Basic Tutorials of Redis(5)
    Basic Tutorials of Redis(4) -Set
    Basic Tutorials of Redis(3) -Hash
    Basic Tutorials of Redis(2)
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2621727.html
Copyright © 2020-2023  润新知