• Prime Ring Problem


    Prime Ring Problem

    Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 644 Accepted Submission(s): 382
     
    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
     
     
    Source
    Asia 1996, Shanghai (Mainland China)
     
    Recommend
    JGShining
     
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <string.h>
    using namespace std;
    #define N 50
    int flag[N];
    int prime[51];
    int ok[50];
    int vis[25];
    int path[25];
    int f=0;
    int n;
    void init()
    {
        memset(flag,0,sizeof(flag)); //全部置为0
        int q = 0; //prime数组的下标
        int i;
        for(i = 2;i * i < N;i++)
        {
            if(flag[i]) continue; //表示i为前面某个数的倍数,肯定不是素数
            prime[q++] = i;
            for(int j = i * i;j < N;j += i) //将是i倍数的全部筛掉
            {
                flag[j] = 1;
            }
        }
        for(i;i <= N;i++) //从i统计到N便是求得的2——N内的素数
         {
            if(flag[i] == 0) prime[q++] = i;
        }
        for(i = 0;i < 15;i++) //打印前25个素数供你检查,就是100以内的那25个素数
        {
            ok[prime[i]]=1;
        }
    }
    void print(int i)
    {
         if(f==0)
        {
            printf("%d",i);
            f=1;
        }
        else
        {
            printf(" %d",i);
        }
        if(path[i]==i)
        return;
        print(path[i]);
    }
    void dfs(int x,int s)
    {
        if(s==1&&ok[x+1])
        {
            f=0;
            print(1);
            printf("
    ");
            return;
        }
        for(int i=2;i<=n;i++)
        {
            if(vis[i]) continue;
            if(ok[i+x])
            {
                path[x]=i;
                vis[i]=1;
                dfs(i,s-1);
                vis[i]=0;
                path[i]=i;
            }
        }
        return;
    }
    int main()
    {
        memset(vis,0,sizeof (vis));
        memset(ok,0,sizeof (ok));
        init();
        for(int i=0;i<=20;i++)
            path[i]=i;
        int c=1;
        while(scanf("%d",&n)!=EOF)
        {
            printf("Case %d:
    ",c++);
            dfs(1,n);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    数据库备份 DBS(Database Backup),知识点
    关系型数据库 RDS(Relational Database Service),知识点
    对象存储服务 OSS(Object Storage Service),知识点(待补充上仓库代码)
    Java 为什么需要包装类,如何使用包装类?
    for each 语句
    缓存中,2个注解:@cacheable 与 @cacheput 的区别
    微信小程序,相关代码
    微信小程序中的事件
    通俗易懂:索引、单列索引、复合索引、主键、唯一索引、聚簇索引、非聚簇索引、唯一聚簇索引 的区别与联系
    MySQL 的各种 join
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6067343.html
Copyright © 2020-2023  润新知