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 ...
随机推荐
- Linux tomcat
一.简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选.对于一个初学者来说 ...
- 没加载redis类,却可以实例化redis
原因:phpinfo里面已有redis扩展
- 专访UI中国认证设计师卤大湿 | 一位UI大师关于UI设计的思考
现如今,设计师可以说是一个自带光环的Title,很多深藏不漏的UI设计师们都在以自己的方式为产品设计做出贡献,卤大湿便是这其中之一. 精分青年卤大湿,这个在UI中国上是张酷酷的鲁迅头像的UI设计师,是 ...
- c++11 stl 学习之 shared_ptr
shared_ptr智能指针 shared_ptr 的声明初始化方式由于指针指针使用explicit参数 必须显示声明初始化shared_ptr<string> pNico = new s ...
- 状态机学习(三)解析JSON1
来自 从零开始的 JSON 库教程 从零开始教授如何写一个符合标准的 C 语言 JSON 库 作者 Milo Yip https://zhuanlan.zhihu.com/json-tutorial ...
- QueryRunner类的八种结果处理集
package cn.jy.demo; import java.sql.Connection; import java.sql.SQLException; import java.util.List; ...
- HttpURLConnection 返回汉字乱码(全是问号)
public static String doPost(String urlStr, Map<String, Object> paramMap) throws Exception { UR ...
- jdbc元数据 以及自己动手写一个curd框架
数据库元数据(MetaData):数据库存储结构定义信息 (库.表.列 定义信息) ParameterMetaData 参数元数据 ---- 获得预编译SQL语句中 ? 信息 getParamet ...
- 利用PHP脚本辅助MySQL数据库管理2-表主键表索引
<?php $dbi = new DbMysql; $dbi->dbh = 'mysql://root:mysql@127.0.0.1/coffeetest'; $map = array( ...
- 码云-中国的github
# 域名城 https://www.domain.cn/ # 码云 https://gitee.com