uva 524 prime ring problem——yhx
Prime Ring Problem |
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 <= 16)
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.
You are to write a program that completes above process.
#include<cstdio>
#include<cstring>
bool prm[],vis[];
int a[],n;
bool ck(int x)
{
int i;
for (i=;i*i<=x;i++)
if (x%i==) return ;
return ;
}
void dfs(int p)
{
int i,j,k,x,y,z;
if (p==n+)
{
if (prm[a[n]+a[]])
{
printf("%d",a[]);
for (i=;i<=n;i++)
printf(" %d",a[i]);
printf("\n");
}
return;
}
for (i=;i<=n;i++)
if (vis[i]==&&prm[i+a[p-]])
{
a[p]=i;
vis[i]=;
dfs(p+);
vis[i]=;
}
}
int main()
{
int i,j,k,p,q,x,y,z,t;
bool bbb=;
for (i=;i<=;i++)
prm[i]=ck(i);
a[]=;
t=;
while (scanf("%d",&n)==)
{
if (bbb) printf("\n");
bbb=;
memset(vis,,sizeof(vis));
vis[]=;
printf("Case %d:\n",++t);
dfs();
}
}
素数环。注意边界。注意每组数据间的回车(虽然题上没说)。
uva 524 prime ring problem——yhx的更多相关文章
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- UVA - 524 Prime Ring Problem(素数环)(回溯法)
题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...
- UVa 524 Prime Ring Problem【回溯】
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- UVa 524 - Prime Ring Problem
题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
- Uva 524 Prime Ring
如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候 首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立 还有要记得vis的使用和递归之后的清理. # ...
随机推荐
- idea中maven项目xml资源文件无法读取
解决方法:编辑pom.xml文件 添加如下标签 <build> <resources> <resource> <directory>src/main/j ...
- HttpController的激活
Web API调用请求的目标是定义在某个HttpController类型中的某个Action方法,所以消息处理管道的最终需要激活目标HttpController对象.调用请求的URI会携带目标Http ...
- elasticsearch分词插件的安装
IK简介 IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本.最初,它是以开源项目Luen ...
- Snort - manual 笔记(三)
1.6 Reading pcap files Snort 不仅可以监听interface, 还可以读取和分析已经捕获的数据包. 1.6.1 Command line arguments 下面的命令都可 ...
- https和http
今天登网站的时候用https登的,没登上去用http找到网站了,于是就去百度了下他俩的区别简单的画了俩图.
- 在Android Studio中用Gradle添加Robolectric
我们用Robolectric测试的话需要在gradle中进行配置,国内的详细教程太过简易,而且很多是低版本下的配置方案.所以经过本人的仔细摸索,找到了现在高版本中的配置方案,主要还是参考了官网的配置教 ...
- 安卓开发_浅谈ContextMenu(上下文菜单)
长下文菜单,即长按view显示一个菜单栏 与OptionMenu的区别OptionMenu对应的是activity,一个activity只能拥有一个选项菜单ContextMenu对应的是View,每个 ...
- storyBoard配置错误导致崩溃 superview]: unrecognized selector...
控制台打印崩溃原因 [TaskStartVC superview]: unrecognized selector sent to instance RT TaskStartVC是一个同storyBoa ...
- iOS设计模式之单例模式
单例模式 基础理解 所有类都有构造方法,不编码则系统默认生成空的构造方法,若有显示定义的构造方法,默认的构造方法就会失效. 单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全 ...
- OC中几种集合的遍历方法(数组遍历,字典遍历,集合遍历)
// 先分别初始化数组.字典和集合,然后分别用for循环.NSEnumerator枚举器和forin循环这三个方法来实现遍历 NSArray *array = @[@"yinhao" ...