博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-1016 Prime Ring Problem DFS
阅读量:7194 次
发布时间:2019-06-29

本文共 844 字,大约阅读时间需要 2 分钟。

  简单DFS,需要注意的是最后的那个数加上一要是个素数。

  代码如下:

#include 
#include
#include
#include
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;}

转载地址:http://yotkm.baihongyu.com/

你可能感兴趣的文章
[译]JavaScript中对象的属性
查看>>
vimmate
查看>>
Drupal 7 安装 TinyMCE。
查看>>
浅谈SQL SERVER中事务的ACID
查看>>
Android中的DownloadManager
查看>>
[服务器开发]可伸缩系统的设计模式(译)
查看>>
【短语学习】狮子那一份the lions share
查看>>
poj1011
查看>>
class和struct
查看>>
潇洒人生“二十贵”
查看>>
poj1269
查看>>
压缩、解压缩流GZipStream
查看>>
js编程思路--把相同功能的代码,配置放到一个对象里
查看>>
Oracle性能优化
查看>>
拖动缩放[转]
查看>>
ASP.NET MVC---自定义HtmlHelper方法
查看>>
javaweb入门(4)-- 详细了解http协议2
查看>>
POJ-3686 The Windy's 犀利构图+KM
查看>>
重新认识c++的cin、cout
查看>>
poj1611
查看>>