简单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相关推荐

  1. hdu 1016 Prime Ring Problem(DFS)

    本题链接:点击打开链接 本题大意: 输入一个数n表示须要查找的数有n个,而且这些数连成一个环,随意两个相邻的数之和都为素数. 解题思路: 就是从1開始对每一个点进行查找,符合条件的点就存到一个数组中, ...

  2. 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 ...

  3. HDU1016 Prime Ring Problem dfs+回溯

    点击打开链接 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. 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 ...

  5. 【HDOJ】1016 Prime Ring Problem_天涯浪子_新浪博客

    [题目]http://acm.hdu.edu.cn/showproblem.php?pid=1016 [报告] 素数环问题,要求相邻两个数的和是素数. 标准的暴力DFS问题,注意一下N=1的特殊情况. ...

  6. UVA524 素数环 Prime Ring Problem

    UVA524 素数环 Prime Ring Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/UVA52 ...

  7. UVA - 524:Prime Ring Problem

    Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...

  8. UVA - 524 Prime Ring Problem

    题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as ...

  9. [HDOJ1016]Prime Ring Problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...

最新文章

  1. android 背景图太大,android – 背景图片需要太长时间才能画(Canvas)Jerky Sprites ……?...
  2. Windows8 解决VMware与Hyper-V不兼容共存方法
  3. canvas使用技巧大全
  4. 高性能计算的线程模型:Pthreads 还是 OpenMP
  5. 【软考-软件设计师】总线结构
  6. Android学习笔记44:JSON数据解析
  7. 再见2019,拥抱2020
  8. HDU 1027 全排列
  9. Unity3D导出的EXE不用显示分辨率选择界面
  10. 安装WindowBuilder后,新建JFrame文件,不能查看Design
  11. MAC编译:fatal error: ‘endian.h‘ file not found
  12. 《从0到1:CTFer成长之路》
  13. 跨平台的会员通 打通品牌任督二脉
  14. 阿里云域名注册流程(全流程详细教程)
  15. 【文字】文字消散效果
  16. 记录虚拟机中更改虚拟网络编辑器无效的解决方法
  17. win8下载java_jdk,64位下载win8
  18. WIN 11 无法正常运行HCL 模拟器
  19. java二维数组三种初始化方法(实例)
  20. 生成用符号拼成的字符//字符画

热门文章

  1. ElasticSearch 知识点整理(深入)
  2. slf4j介绍及配置详解
  3. 工业界推荐系统实用分析技巧
  4. Mysql 索引底层原理
  5. redis Hash槽
  6. 深入理解java垃圾回收机制
  7. schema约束文档与xml文件详解
  8. TPYBoard v102 DIY照相机(视频和制作流程)
  9. 快速开发基于 HTML5 网络拓扑图应用之 DataBinding 数据绑定篇
  10. JS简单正则得到字符串中特定的值