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.



Ring Problem" title="Prime Ring Problem">
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
题意:给你一个n,得到一个数组,1~n;让你写出一个素数环,要求相邻两个数(顺逆时针)和是素数,输出的时候第一个永远是一;
解题思路:按照递归求全排列的思想,从第2个开始递归一直到最后一位,并且首尾也是素数才算是搜索完成;
感悟:一开始我还以为得剪纸,但是只有20个数,一次就过了;
代码:
#include

#include

#include

#include

#define maxn 25

using namespace std;

int ans[maxn],visit[maxn],n;

int Prime(int a)

{

    for(int
i=2;i<=sqrt(a);i++)

       
if(a%i==0)

           
return 0;

    return
1;

}



void dfs(int cur)

{

   
if(cur==n&&Prime(ans[0]+ans[n-1]))//递归到最后一位,并且首尾也能是素数

    {

       
for(int i=0;i

           
printf("%d ",ans[i]);

       
printf("%d\n",ans[n-1]);

    }

    else

    {

       
for(int i=2;i<=n;i++)

       
{

           
if(!visit[i]&&Prime(i+ans[cur-1]))//这个数没用过并且相邻的是素数

           
{

               
ans[cur]=i;

               
visit[i]=1;

               
dfs(cur+1);

               
visit[i]=0;

           
}

       
}

    }

}

int main()

{

   
//freopen("in.txt", "r", stdin);

    int
s=1;

   
memset(visit,0,sizeof(visit));

   
while(~scanf("%d",&n)&&n)

    {

       
for(int i=0;i

           
ans[i]=i+1;

       
printf("Case %d:\n",s++);

       
dfs(1);

       
printf("\n");

    }

}

Prime Ring Problem的更多相关文章

  1. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  2. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  9. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  10. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

随机推荐

  1. webpack + vue + node 打造单页面(入门篇)

    1.node下载地址:http://nodejs.cn/download/,安装完成检查node和npm版本 2.淘宝镜像 : npm install cnpm -g --registry=https ...

  2. Python 接口测试(五)

    五:使用python进行组织编写接口测试用例 接口测试其实就是几个步骤. 拿到接口的url地址 查看接口是用什么方式发送 添加请求头,请求体 发送查看返回结果,校验返回结果是否正确 明白了接口测试的测 ...

  3. Angular2开发拙见

    本文集中讲讲笔者目前使用ng2来开发项目时对其组件的使用的个人的一些拙劣的经验. 先简单讲讲从ng1到ng2框架下组件的职责与地位: ng1中的一大特色--指令,分为属性型.标签型.css类型和注释型 ...

  4. python基础之五大标准数据类型

    学习一门语言,往往都是从Hello World开始. 但是笔者认为,在一个黑框框中输出一个"你好,世界"并没有什么了不起,要看透事物的本质,熟悉一门语言,就要了解其底层,就是我们常 ...

  5. SQLServer总结

    基础 nvarchar 和 varchar等的区别 1.nvarchar多了一个N,n表示使用的unicode编码,不用N开头的是用的utf-8编码. 2.所以中文在varchar中占两个字符长度,在 ...

  6. 安装myeclipse2015 stable 3.0破解之后发生出现SECURITY ALERT:iNTEGRITY CHECK ERROR然后闪退解决方案

    安装好myeclipse2015 stable以后也一步步按着破解文件的步骤来进行.打开myEclipse---->Subscription information--->Subscrip ...

  7. 实例讲解js正则表达式的使用

    前言:正则表达式(regular expression)反反复复学了多次,学了又忘,忘了又学,这次打算把基本的东西都整理出来,加强记忆,也方便下次查询. 学习正则表达式之前首先需要掌握记忆这些基本概念 ...

  8. ArrayList , Vector 数组集合

    ArrayList 的一些认识: 非线程安全的动态数组(Array升级版),支持动态扩容 实现 List 接口.底层使用数组保存所有元素,其操作基本上是对数组的操作,允许null值 实现了 Randm ...

  9. ionic2+Angular2:套接口明细步骤,以登录功能为例

    1.在app.module.ts引用HttpModul,并在imports内引用.截图如下:   2.在src目录下新建http服务.命令行:ionic g provider HttpService ...

  10. PHP计算上个月的开始时间和结束时间戳

    $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); $t = date('t',strtotime($m)); //上个月共多少天 $ ...