hdu-1016-Prime Ring Problem

简介: hdu-1016-Prime Ring Problem


Prime Ring Problem

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


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>
int n,m,a[10],arr[10],mark[10];
void dfs(int v){
    if(v >= n){
        for(int i = 0;i < n;i++)
            printf("%d ",a[i]);
        printf("\n");
        return ;
    }
    for(int i = 0; i< n;i++){
            mark[i]=1;
            dfs(v+1);
            mark[i] = 0;  
        }
    }
}
int main(){
    while(scanf("%d",&n)==1){
      int i;
        memset(mark,0,sizeof(mark));
        for(i=0;i<n;i++)
            arr[i]=i+1;
        dfs(0);
    }        
}
 
 
 
 
 
#include<cstdio>
#include<cstring>
int n;
int a[21],mark[21];
int prime[] = {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,0,0,0}; //这里是前40的素数表
void dfs(int v)
{
    if(v==n+1)
    {
        if(prime[a[v-1]+1])//这里记得判断首尾相加是不是素数
        {
            for(int i=1;i<n;i++)
               printf("%d ",a[i]);
               printf("%d",a[n]);
               printf("\n");
        }
    }
    else
    {
        for(int i=2;i<=n;i++)
        {
            if(!mark[i]&&prime[a[v-1]+i]) //  控制相加是素数
            {
                mark[i]=1;
                a[v]=i;
                dfs(v+1);
                mark[i]=0;
            }
        }
    }
}
int main()
{
    int k=1;
    while(~scanf("%d",&n))
    {
        printf("Case %d:\n",k++);
        memset(mark,0,sizeof(mark));
        a[1]=1;
        dfs(2);
        printf("\n");
    }
 return 0;
}






目录
相关文章
|
12月前
|
JSON 应用服务中间件 API
【干货】将参数传递给 Apache APISIX 的五种方式
【干货】将参数传递给 Apache APISIX 的五种方式
259 0
|
定位技术
|
Go
The “gopls“ command is not available. Run “go get -v golang.org/x/tools/gopls“ to install.【已解决】
The “gopls“ command is not available. Run “go get -v golang.org/x/tools/gopls“ to install.【已解决】
227 3
|
数据可视化 前端开发 搜索推荐
数据可视化大屏百度地图GPS轨迹位置感知状态开发实战案例解析(百度地图jsAPI,包含缩放控件、点线覆盖物、弹窗、标注图标分类功能)
数据可视化大屏百度地图GPS轨迹位置感知状态开发实战案例解析(百度地图jsAPI,包含缩放控件、点线覆盖物、弹窗、标注图标分类功能)
514 0
|
存储 程序员 C语言
初识c语言系列-2-常,变量,字符(串),转义字符和注释
初识c语言系列-2-常,变量,字符(串),转义字符和注释
初识c语言系列-2-常,变量,字符(串),转义字符和注释
|
存储 运维 Linux
2.docker架构,原理,安装及简单应用
docker的架构 先来看docker官网给出的docker架构图:
404 1
2.docker架构,原理,安装及简单应用
|
消息中间件 监控 前端开发
我有 7种 实现web实时消息推送的方案,7种!
我有 7种 实现web实时消息推送的方案,7种!
1022 2
|
关系型数据库 MySQL 数据库连接
nacos进阶二--使用源码启动项目
nacos进阶二--使用源码启动项目
1080 0
nacos进阶二--使用源码启动项目
|
Nacos
Nacos技术分享
Nacos技术分享
187 0
Nacos技术分享