Prime Ring Problem
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">< 20).
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.
1:
6
4
2:
7 4
7 6
3 2
5 2
#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的更多相关文章
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
随机推荐
- webpack + vue + node 打造单页面(入门篇)
1.node下载地址:http://nodejs.cn/download/,安装完成检查node和npm版本 2.淘宝镜像 : npm install cnpm -g --registry=https ...
- Python 接口测试(五)
五:使用python进行组织编写接口测试用例 接口测试其实就是几个步骤. 拿到接口的url地址 查看接口是用什么方式发送 添加请求头,请求体 发送查看返回结果,校验返回结果是否正确 明白了接口测试的测 ...
- Angular2开发拙见
本文集中讲讲笔者目前使用ng2来开发项目时对其组件的使用的个人的一些拙劣的经验. 先简单讲讲从ng1到ng2框架下组件的职责与地位: ng1中的一大特色--指令,分为属性型.标签型.css类型和注释型 ...
- python基础之五大标准数据类型
学习一门语言,往往都是从Hello World开始. 但是笔者认为,在一个黑框框中输出一个"你好,世界"并没有什么了不起,要看透事物的本质,熟悉一门语言,就要了解其底层,就是我们常 ...
- SQLServer总结
基础 nvarchar 和 varchar等的区别 1.nvarchar多了一个N,n表示使用的unicode编码,不用N开头的是用的utf-8编码. 2.所以中文在varchar中占两个字符长度,在 ...
- 安装myeclipse2015 stable 3.0破解之后发生出现SECURITY ALERT:iNTEGRITY CHECK ERROR然后闪退解决方案
安装好myeclipse2015 stable以后也一步步按着破解文件的步骤来进行.打开myEclipse---->Subscription information--->Subscrip ...
- 实例讲解js正则表达式的使用
前言:正则表达式(regular expression)反反复复学了多次,学了又忘,忘了又学,这次打算把基本的东西都整理出来,加强记忆,也方便下次查询. 学习正则表达式之前首先需要掌握记忆这些基本概念 ...
- ArrayList , Vector 数组集合
ArrayList 的一些认识: 非线程安全的动态数组(Array升级版),支持动态扩容 实现 List 接口.底层使用数组保存所有元素,其操作基本上是对数组的操作,允许null值 实现了 Randm ...
- ionic2+Angular2:套接口明细步骤,以登录功能为例
1.在app.module.ts引用HttpModul,并在imports内引用.截图如下: 2.在src目录下新建http服务.命令行:ionic g provider HttpService ...
- PHP计算上个月的开始时间和结束时间戳
$m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); $t = date('t',strtotime($m)); //上个月共多少天 $ ...