HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem
Note: the number of first circle should always be 1.
Inputn (0 < n < 20).
OutputThe 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例外,但是本题不会出现两个数的和为2)不是素数,
所以素数环里奇偶间隔。如果n是奇数,必定有两个奇数相邻的情况。
所以当n为奇数时,输出“No Answer”。
当n == 1时只1个数,算作自环,输出1
所有n为偶数的情况都能变成奇偶间隔的环-----所以都有结果。
#include<stdio.h>
#include<string.h>
int n,c=,i;
int a[],b[];
int jo(int a)
{
return a%==?:;
}
int prime[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
void dfs(int step)
{
int i;
if(step>n&&prime[a[]+a[n]]){
for(i=;i<=n;++i){
if(i==) printf("%d",a[i]);
else printf(" %d",a[i]);
}
printf("\n");
return;
}
if(jo(a[step-])){
for(i=;i<=n;i+=){
if(!b[i]&&prime[a[step-]+i]){
b[i]=;
a[step]=i;
dfs(step+);
b[i]=;
}
}
}
else{
for(i=;i<=n;i+=){
if(!b[i]&&prime[a[step-]+i]){
b[i]=;
a[step]=i;
dfs(step+);
b[i]=;
}
}
}
}
int main()
{
while(~scanf("%d",&n)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
a[]=;b[]=;
if(n==) printf("Case %d:\n1\n\n",++c);
else if(!jo(n)){
printf("Case %d:\n",++c);
dfs();
printf("\n");
}
}
return ;
}
HDU - 1016 Prime Ring Problem 经典素数环的更多相关文章
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- 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 ...
- 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(深度优先搜索)
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 ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- kubernetes高级之pod安全策略
系列目录 什么是pod安全策略 pod安全策略是集群级别的用于控制pod安全相关选项的一种资源.PodSecurityPolicy定义了一系列pod相要进行在系统中必须满足的约束条件,以衣一些默认的约 ...
- 李洪强iOS开发之 - 项目架构
李洪强iOS开发之 - 项目架构 01 - 在Appdelegate中设置跟控制器 //导入头文件
- sublime常用的插件
Sublime Text常用插件 1.Package Control 快捷键ctrl+~调出Sublime Text控制台,然后输入以下代码(Sublime Text3)安装Package Contr ...
- 《学习opencv》笔记——矩阵和图像操作——cvConvertScale,cvConvertScaleAbs,cvCopy and cvCountNonZero
矩阵和图像的操作 (1)cvConvertScale函数 其结构: void cvConvertScale( //进行线性变换,将src乘scale加上shift保存到dst const CvArr* ...
- 继续聊WPF——获取ComboBox中绑定的值
千万不要认为WPF中的数据绑定会很复杂,尽管它的确比Winform程序灵活多了,但其本质是不变的,特别是ComboBox控件,我们知道在Winform中对该控件的有两个专为数据绑定而设定的属性——Di ...
- 微信分享配置(js-sdk)
现在的微信分享给朋友-分享到朋友圈 链接带有自定义的title.描述.图片,需要配置js-sdk(地址:mp.weixin.qq.com)微信文档 需要后台配置config的参数,返回给前台 1)de ...
- html5 file 上传图片
说明:开发环境 vs2012 mvc4项目,后台语言csharp 1.前端代码 <html xmlns="http://www.w3.org/1999/xhtml"> ...
- ElasticSearch(三)mac安装
1.首先要安装jdk 2.到官网或是用brew下载ElasticSearch 安装包,这边我们选择在官网下载对应的安装包 https://www.elastic.co/cn/downloads/ela ...
- Thumbelina,摘自iOS应用Snow White and more stories
Once upon a time there was a woman who wanted to have a child. 从前,有个想要个孩子的女人. A witch heard her wish ...
- Ubuntu安装搜狗输入法笔记
1.移除ibus sudo apt-get purge ibussudo apt-get autoremove 2.安装fcitx sudo apt-get install fcitx 3.安装搜狗输 ...