hdu 素数环
算法:搜索
题意:相邻的两个数之和是素数,别忘了最后一个,和第一个
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
6
8
Sample Output
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 <string>
#include <algorithm>
#include <iomanip>
#include <cstring>
using namespace std;
int a[30],b[45],n,m,c[30];
void cmp()
{ memset(b,1,sizeof(b));
b[1]=b[0]=0;
for(int i=2;i<=22;i++)
{
for(int j=2*i;j<45;j=j+i)
b[j]=0;
}
}
void dfs(int x,int k)
{
if(k==n&&b[c[0]+c[n-1]])
{
for(int i=0;i<n;i++)
{
if(i)cout<<" ";
cout<<c[i];
}
cout<<endl;
}
else
{
for(int i=2;i<=n;i++)
{
if(!a[i]&&b[i+c[k-1]])
{
a[i]=1;
c[k]=i;
dfs(n,k+1);
a[i]=0;
}
}
}
}
int main()
{
int i,j=0;
cmp();
while(cin>>n)
{
for(i=1;i<=n;i++)
a[i]=0;
c[0]=1;
cout<<"Case "<<++j<<":"<<endl;
dfs(n,1);
cout<<endl;
}
return 0;
}
hdu 素数环的更多相关文章
- HDU 1016 素数环(dfs + 回溯)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 一道很典型的dfs+回溯: 根据题意首先进行初始化,即第一个位置为1,然后进行dfs, ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- nyoj 488 素数环
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
- nyoj 素数环
算法:搜索 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输 ...
- nyoj素数环
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
- ny488 素数环
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起 ...
- DFS搜索题素数环
素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...
- ACM_素数环(dfs)
Problem Description: 如图所示,环由n个圆组成. 将自然数1,2,...,n分别放入每个圆中,并且两个相邻圆中的数字总和应为素数. 注意:第一个圆圈的数量应该始终为1. Input ...
- EOJ 1114 素数环
题意 一个由自然数 1…n (n≤18) 素数环就是如下图所示,环上任意两个节点上数值之和为素数. 1 / \ 4 2 \ / 3 Input 输入只有一个数 n,表示你需要建立一个 1… ...
随机推荐
- c++回调实现
回调是A将一个函数指针传给B,然后调用B,B在执行自身函数后,再在合适的时候执行A的这个函数指针,这样就能避免A和B的相互包含和链接,在大型项目中回调是一种打破循环依赖的常用技术. typedef v ...
- mysql中取系统当前时间
<select id="getFreightEfclInventoryList" parameterType="long" resultMap=" ...
- python 3.5 格式化字符串输出
#!/usr/bin/env python #encoding: utf-8 #.strip('里面可以去掉字符串中两边的字符') name = input('name :').strip(' ') ...
- GCD使用经验与技巧浅谈--备
GCD(Grand Central Dispatch)可以说是Mac.iOS开发中的一大“利器”,本文就总结一些有关使用GCD的经验与技巧. dispatch_once_t必须是全局或static变量 ...
- VC 隐藏托盘图标
苦苦寻找的隐藏托盘图标的方法,今天终于搞定,献给大家! #include <atlbase.h> #include <atlconv.h> #include <CommC ...
- Sql 高效分页
http://www.111cn.net/database/mysql/46350.htm http://g.kehou.com/t1032617472.html http://www.cnblogs ...
- BZOJ1978: [BeiJing2010]取数游戏 game
1978: [BeiJing2010]取数游戏 game Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 650 Solved: 400[Submit] ...
- jQuery的animate方法在IE8下出现小问题
今天修改网站的bug,把网页显示的几张图片给做成左右滑动的动画效果: 由于本身有一个demo可供参考,然后在此基础上进行修改,所以很快就搞定了,然后在chrome,firefox,IE9下分别进行测试 ...
- .net中除去IList中的多余项
IList<ActionInfo> tempList = new List<ActionInfo>(); IList<ActionInfo> tempActionL ...
- 黑马程序员_Java面向对象1_封装
3.面向对象_封装 3.1面向对象概念 3.1.1理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程:强调的是功能行为(执行者) 面向对象:将功能封装进对象,强调具备了 ...