HDU-1016 Prime Ring Problem DFS
简单DFS,需要注意的是最后的那个数加上一要是个素数。
代码如下:
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;int hash[25], rec[25], cnt;bool isprime( int x )
{if( x<= 1 ) return false;if( x== 2 ) return true;int lim= sqrt( x );for( int i= 2; i<= lim; ++i ){if( x% i== 0 ) return false;}return true;
}void DFS( int last, int cnt, int N )
{if( cnt== N ){if( isprime( rec[N- 1]+ 1 ) ){for( int i= 0; i< cnt; ++i ){printf( i== 0? "%d": " %d", rec[i] );}puts( "" );}return;}for( int i= 1; i<= N; ++i ){if( !hash[i]&& isprime( last+ i ) ){rec[cnt]= i;hash[i]= 1;DFS( i, cnt+ 1, N );hash[i]= 0;}}
}int main()
{int N, ca= 0;while( scanf( "%d", &N )!= EOF ){rec[0]= 1;hash[1]= 1;printf( "Case %d:\n", ++ca );DFS( 1, 1, N );puts( "" );}return 0;
}
转载于:https://www.cnblogs.com/Lyush/archive/2011/08/11/2135494.html
HDU-1016 Prime Ring Problem DFS相关推荐
- hdu 1016 Prime Ring Problem(DFS)
本题链接:点击打开链接 本题大意: 输入一个数n表示须要查找的数有n个,而且这些数连成一个环,随意两个相邻的数之和都为素数. 解题思路: 就是从1開始对每一个点进行查找,符合条件的点就存到一个数组中, ...
- HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述: Prime Ring Problem Time Limit: 4000/2000 ...
- HDU1016 Prime Ring Problem dfs+回溯
点击打开链接 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdoj - 1258 Sum It Up hdoj - 1016 Prime Ring Problem (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. 1 #include <cstdio> 2 #i ...
- 【HDOJ】1016 Prime Ring Problem_天涯浪子_新浪博客
[题目]http://acm.hdu.edu.cn/showproblem.php?pid=1016 [报告] 素数环问题,要求相邻两个数的和是素数. 标准的暴力DFS问题,注意一下N=1的特殊情况. ...
- UVA524 素数环 Prime Ring Problem
UVA524 素数环 Prime Ring Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/UVA52 ...
- UVA - 524:Prime Ring Problem
Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...
- UVA - 524 Prime Ring Problem
题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as ...
- [HDOJ1016]Prime Ring Problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...
最新文章
- android 背景图太大,android – 背景图片需要太长时间才能画(Canvas)Jerky Sprites ……?...
- Windows8 解决VMware与Hyper-V不兼容共存方法
- canvas使用技巧大全
- 高性能计算的线程模型:Pthreads 还是 OpenMP
- 【软考-软件设计师】总线结构
- Android学习笔记44:JSON数据解析
- 再见2019,拥抱2020
- HDU 1027 全排列
- Unity3D导出的EXE不用显示分辨率选择界面
- 安装WindowBuilder后,新建JFrame文件,不能查看Design
- MAC编译:fatal error: ‘endian.h‘ file not found
- 《从0到1:CTFer成长之路》
- 跨平台的会员通 打通品牌任督二脉
- 阿里云域名注册流程(全流程详细教程)
- 【文字】文字消散效果
- 记录虚拟机中更改虚拟网络编辑器无效的解决方法
- win8下载java_jdk,64位下载win8
- WIN 11 无法正常运行HCL 模拟器
- java二维数组三种初始化方法(实例)
- 生成用符号拼成的字符//字符画