hdoj--1016<dfs>
题目链接: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>的更多相关文章
- hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...
- 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 ...
- hdoj 1016 Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- HDOJ 1312 DFS&BFS
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1026 dfs路径保存
#include<cstdio> #include<cstring> #include<cmath> ][]; #define inf 0xffffff int n ...
- HDOj 1010 DFS优化
#include<cstdio> #include<cstring> ]={,,,-}; ]={,,-,}; ][]; int x1,y1,x2,y2; int step; i ...
- HDU 1016 DFS
很简单的深搜 只要看出来是深搜... 注意判断最后一点是否与加一为质数 #include<stdio.h> #include<string.h> #include<alg ...
- HDOJ 2212 DFS
Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- HDOJ 1427(dfs) 速算24点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...
随机推荐
- 3D模型预处理(格式转换:obj转换为gltf)
在cesium中导入模型需要的是gltf或glb格式的文件,cesium官方提供了obj转gltf文件的工具,一个obj2gltf的库,地址为https://github.com/Analytical ...
- OpenGL小试牛刀第二季(粒子模拟)
效果截图:粒子模拟代码展示:#include "Particle.h" /** 构造函数 */CParticle::CParticle(){ data = NULL; numpar ...
- HTML5中的SSE(服务器推送技术)
本文原链接:https://cloud.tencent.com/developer/article/1194063 SSE技术详解:一种全新的HTML5服务器推送事件技术 前言 概述 基本介绍 与We ...
- Kafka 完全分布式集群环境搭建
思路: 先在主机s1上安装配置,然后远程复制到其它两台主机s2.s3上, 并分别修改配置文件server.properties中的broker.id属性. 1. 搭建前准备 示例共三台主机,主机IP映 ...
- perl -p -i -w -e
.txt kllk nciuwbufcbew``````//.]];s[[..; klklkl x,dsncdk,;l,ex xw,eocxmcmck .txt .txt kkkkkkkkkkkkkk ...
- WinForm中Timer倒计时
添加一个Timer控件: 在初始化代码中 public Form1() { InitializeComponent(); button_Read.Enabled = false; button_Sta ...
- Confirm the Ending--freecodecamp算法题目
Confirm the Ending(检查字符串结尾) 要求 判断一个字符串(str)是否以指定的字符串(target)结尾. 如果是,返回true;如果不是,返回false. 思路 利用.repla ...
- c++ 函数指针应用,定义一个方法,传入两个参数和一个函数指针,并返回结果
#include <iostream> #include <string> using namespace std; double add(double x, double y ...
- NOIP模拟赛 不等数列
[题目描述] 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入“>”和“<”.问在所有排列中,有多少个排列恰好有k个“<”.答案对2012取模. [输入格式] 第一行 ...
- MySQL在windows上的安装步骤
参考文章MySQL安装及建议:https://zhuanlan.zhihu.com/p/44977117 但在进入mysql中修改root命令时,使用文章中的命令: ALTER USER 'root' ...