杭电oj 1016 Prime Ring Problem
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23458 Accepted Submission(s):
10465
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.

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.
8
这一题的意思是输入一个数n,然后让你用1~n的数围成一个圈,但是每相邻的两个数的和必须为素数。
#include <iostream>
#include <cmath>
using namespace std;
#define MAX 22
int a[MAX]; //标记数组
int b[MAX];
int n;
bool prime(double n)
{
int i, m;
m = (int)sqrt(n);
for (i=; i<=m; i++)
if ((int)n%i == )
return false;
return true;
}
void dfs(int i)
{
int j;
if (i>=n)
{
if (prime(double(b[n-]+))) //判断最后一个和第一个数的和是不是素数
{
cout<<b[];
for (j=; j<n; j++)
cout<<" "<<b[j];
cout<<endl;
}
}
else
{
for (j=; j<=n; j++)
{
if (a[j] || !prime(double(b[i-]+j))) //a[j]已经被加入到圈中或者相邻两个数和不是素数,则continue
continue;
a[j] = ; //如果j已经加入圈中,则标记为1
b[i] = j;
dfs(i+);
a[j] = ;
}
}
}
int main()
{
int i=, j;
while (cin>>n)
{
memset(a,,sizeof(a)); //全置为0
cout<<"Case "<<i++<<":"<<endl;
b[] = ;
dfs();
cout<<endl;
}
return ;
}
杭电oj 1016 Prime Ring Problem的更多相关文章
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- 杭电 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- HBase的架构以及各个模块的功能
一:整体架构 1.体系结构 2.物理模型 3.存储体系 regionserver->region->多个store(列簇)->一个memstore和多个storefile 4.HDF ...
- php--求几个数中的最小值
<?phpecho min(2, 3, 1, 6, 7); // 1echo min(array(2, 4, 5)); // 2echo min(0, 'hello'); // 0ec ...
- php练习:每5个商品一排
老板说: 我要一行5个商品,每个长得都不一样 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- JS:checkFrom对输入框和文本框的判断总结
天看了老东家的一个专题页面,发现里边的checkFrome.js收集了很多对文本框的判断,非常有用收藏一下.其中包含了:1.页面截取字符串2.文本框最大长度限制3.判断必须是数字和字母的组合4.判断是 ...
- Selenium2学习-021-WebUI自动化实战实例-019-设置浏览器窗口位置大小
前文简略讲述了如何获取浏览器窗口的位置和大小,此文讲述如何通过 webdriver 设置浏览器窗口的位置和大小. 直接上码了...... /** * Set browser size for expe ...
- Excl 的一些用法--如何给很多列赋同一个值
1.用鼠标选定要负责的列(多列) 2.在处填写值 3.Ctrl+Enter
- .net中如何使用cookie
比如建立一个名为aspcn,值为灌水小鱼的cookie HttpCookie cookie = new HttpCookie["aspcn"];cookie.Value = &qu ...
- XMPP语音聊天解决方案 (待论证 正打算写)
1.获取音频文件路径 例如: NSURL *recordedFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendin ...
- jQuery选择器总结(转)
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- 微信公开课(北京站)速记 微信、微信支付、O2O的定义与关联
本文为4月29日微信公开课(北京站)微信产品部演讲全文速记,讲述了微信官方对微信.微信支付.O2O的定义与关联等问题的看法与观点. 作者:微信产品部 刘涵涛 吴毅 去年夏天有一个全民打飞机的盛况,这实 ...