题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016

题目描述:1~n的整数排成一个环,首尾相连,相邻的两个数相加是素数,输出满足的排列,1开头输出,字典序;

题目要点:dfs

本题安顺序dfs,可以满足字典序,对于每一个要放进去的数要考察两个,一、是否放过了。二、是否和前面相邻的数构成素数;

所以第一,开一个数组,记录该数是否被放过;第二,写一个判断素数的函数;

代码如下:

(当只有一个元素的时候比较特殊,要考虑到)

 #include<stdio.h>
#include<string.h>
int n,b[],a[],t=;//b数组用来放数;a数组用来记录;
int prime(int x)
{
for(int i=;i<x;i++)
{
if(x%i==)
return ;
}
return ;
}
void dfs(int cur)//cur记录的是这一次放大色是第几个数;
{
if(cur>n) //注意这里一直放到第n+1个,然后比较1和b【n】是否是素数;
{
if(prime(+b[n])){
printf("%d",b[]);
for(int i=;i<=n;i++)
{
printf(" %d",b[i]);
}
printf("\n");
}
}
else
{
for(int i=;i<=n;i++)
{
if(a[i]==&&prime(i+b[cur-]))
{
b[cur]=i;a[i]=i;
dfs(cur+);a[i]=;
} }
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
printf("Case %d:\n",t++);
memset(a,,sizeof(a));
b[]=;//一开头是固定的,直接赋值就好了。
if(n==)//如果只有一个数,算是符合要求直接输出就好;
{
printf("1\n\n");
continue;
}
dfs();//从2开始dfs
printf("\n");
}
return ;
}

hdoj--1016<dfs>的更多相关文章

  1. hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...

  2. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

  3. hdoj 1016 Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  4. HDOJ 1312 DFS&BFS

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  5. HDOJ 1026 dfs路径保存

    #include<cstdio> #include<cstring> #include<cmath> ][]; #define inf 0xffffff int n ...

  6. HDOj 1010 DFS优化

    #include<cstdio> #include<cstring> ]={,,,-}; ]={,,-,}; ][]; int x1,y1,x2,y2; int step; i ...

  7. HDU 1016 DFS

    很简单的深搜 只要看出来是深搜... 注意判断最后一点是否与加一为质数 #include<stdio.h> #include<string.h> #include<alg ...

  8. HDOJ 2212 DFS

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  9. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  10. HDOJ 1427(dfs) 速算24点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...

随机推荐

  1. UVA 1609 Foul Play 不公平竞赛 (构(luan)造(gao)+递归)

    题意:有n支队伍(n是2的整数幂,2<=n<=4),打淘汰赛,胜者进入下一轮,其中1号队伍能打败至少一半的队伍,对于它不能打败的队伍l,一定存在一支它能够打败的队伍w,使得w能直接打败l, ...

  2. 用函数求lnx,lgx等

    https://blog.csdn.net/liujian20150808/article/details/50628061

  3. 获取地址栏参数,json遍历

    1. 获取地址栏参数 GetQueryString: function(name){ // 获取地址栏参数 var reg = new RegExp("(^|&)"+ na ...

  4. Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference

    1. Strong Reference StrongReference 是 Java 的默认引用实现,  它会尽可能长时间的存活于 JVM 内, 当没有任何对象指向它时 GC 执行后将会被回收 @Te ...

  5. SC || Chapter6 复习向 面向可维护性 我哭了

    高内聚低耦合 高内聚:一个模块内部各个元素彼此结合的紧密程度,一个软件模块是由相关性很强的代码组成,只负责一项任务,也就是常说的单一责任原则 低耦合:各模块间相互联系紧密程度,模块间接口的复杂性.调用 ...

  6. 使用filter函数筛选出素数

    function getPrimeNumber(arr) { return arr.filter(function (number) { if (typeof number !== 'number' ...

  7. JS数据结构及算法(二) 队列

    队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...

  8. 获得函数返回值类型、参数tuple、成员函数指针中的对象类型

    //function_traits.h,获得函数返回值类型.参数tuple.成员函数指针中的对象类型 //参考https://github.com/qicosmos/cosmos/blob/maste ...

  9. Redis 和缓存技术

    Redis 是什么?什么作用?优点和缺点? https://blog.csdn.net/weixin_42295141/article/details/81380633 Redis 的主要功能哨兵+复 ...

  10. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...