Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏
Prime Ring Problem
Note: the number of first circle should always be 1.

lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
6
8
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int a[25];//保存结果
int flag[25];//记录一个数是否已用过
bool isprime(int n)
{
if (n == 1 || n == 0)
return 0;
if (n == 2)
return 1;
for (int i = 2; i <= sqrt((double)n); i++)
{
if (n%i == 0)
return 0;
}
return 1;
}
void dfs(int tot)
{
if (tot == n-1)
{
if (isprime(a[n - 1] + a[0]))
{ int o = 0;
for (int i = 0; i < n; i++)
{
while (o++)
{
printf(" ");
break;
}
printf("%d", a[i]);
}
printf("\n");
}
return;
}
for (int i = 2; i <= n; i++)
{
if (flag[i] == 0)
{
if (isprime(a[tot] + i))
{
flag[i] = 1;
a[tot + 1] = i;
dfs(tot + 1);
flag[i] = 0;
}
}
}
}
int main()
{
int k = 1;
while (~scanf("%d", &n))
{
printf("Case %d:\n", k++); a[0] = 1;
memset(flag, 0, sizeof(flag));
dfs(0);
printf("\n");
}
return 0; }
Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏的更多相关文章
- HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏
Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...
- Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...
- 多校赛3- Solve this interesting problem 分类: 比赛 2015-07-29 21:01 8人阅读 评论(0) 收藏
H - Solve this interesting problem Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏
Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...
- Train Problem I 分类: HDU 2015-06-26 11:27 10人阅读 评论(0) 收藏
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
The 3n + 1 problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53927 Accepted: 17 ...
- HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏
Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- Informatica_(5)高级应用
五.高级应用21.任务分区 分区是通过并行处理来提供PowerCenter的执行效率. 分区类型包括:Database partitioning.Hash Auto-keys.Hash User-ke ...
- nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator
该问题是少了一个spring-tx-的jar包,把该包加入到buildpath中就行了. 参考链接:http://blog.csdn.net/Rongbo_J/article/details/4666 ...
- JianShu_failban2实现动态屏蔽的功能
一,首先是服务安装 #vim /etc/yum.repos.d/Centos-Base.repo 在最新新增 [atrpms] name=Red Hat Enterprise Linux $relea ...
- springmvc+mybatis 构建cms+UC浏览器文章功能
最近公司在模拟UC浏览器做一个简单的cms系统,主要针对于企业内部的文章浏览需求,这边考虑用户大多用mobile浏览文章内容,故使用原生的ios和android进行开发,后面也会集成html5. 1. ...
- Behance 大神推荐2019 年所有设计领域的最新趋势!
昨天国内设计界发生了一则重大新闻! 相信大家应该都听说了吧 Behance挂了··· 继续Pinteres之后 在一个设计师不用上班的周六 我的电脑默默打不开Behance了 也就是说大陆地区的ip地 ...
- SSRF-php初探
0x00 前言 1) SSRF的概念很好理解,请自行百度. 2) JAVA/PHP/PYTHON都存在SSRF漏洞(至于其他语言的情况,了解粗浅尚不得知). 3) SSRF的利用方式 ...
- python包与模块导入
一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...
- Ubuntu下Tomcat绑定80端口(zz)
Ubuntu下Tomcat绑定80端口 来源:本站转载 作者:佚名 时间:2011-02-22 TAG: 工作环境迁移到了Ubuntu,很多东西发生了变化,比如原先配置tomcat端口.只需要配置se ...
- 我们用整整三年时间,建成了一套软件:用户定制系统(UD)
这是我们花了三年时间,完成了一套软件--用户定制系统(UD) 主要功能就是集中在下面这个界面了 (自己生成自己哦) ============================= 更多详情,请您访问:我们 ...
- Linux学习笔记:Jenkins安装
操作系统是CentOS 7,安装Jenkins 首先安装jdk,可在Oracle jdk和Openjdk中任选其一安装Oracle jdk步骤见: https://www.cnblogs.com/ ...