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

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
 

  这一题的意思是输入一个数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的更多相关文章

  1. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  2. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  3. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  4. 杭电 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1016 Prime Ring Problem (回溯法)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 关于appstore多语言版本,不可不看!

    http://www.cocoachina.com/appstore/20160513/16256.html

  2. Qt from Linux to Windows target

    45down voteaccepted Just use M cross environment (MXE). It takes the pain out of the whole process: ...

  3. zepto源码--整体框架--学习笔记

    为了深入学习javascript,根据别人推荐的方法之一:研究源码. 相对而言,之前的项目中仅仅使用过zepto和jquery,当前阶段,看到好几千行的jquery源码,心生敬畏,望而却步,所以选择相 ...

  4. VS2013修改MVC4默认生成的模板

    找到以下目录,根据VS版本和安装目录不同相应改动: I:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTempla ...

  5. 去除Html标签

    public static string ParseTags(string Htmlstring)     {         //删除脚本          Htmlstring = Regex.R ...

  6. 监控mysql主从同步状态脚本

    监控mysql主从同步状态脚本 示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroo ...

  7. Eight Popular Open Source Android Game Engines

    https://software.intel.com/en-us/blogs/2012/05/14/eight-popular-open-source-android-game-engines

  8. cp 命令(转)

    cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果是 ...

  9. 一个由INode节点爆满引起的业务故障

    一个由INode节点爆满引起的业务故障 http://2358205.blog.51cto.com/2348205/1747951 好久没有写博文了,今天周六,分享一下刚刚处理完的一个小故障 现象描述 ...

  10. Android 使用PullToRefreshExpandableListView不能setAdapter的问题

    private PullToRefreshExpandableListView lv; lv = (PullToRefreshExpandableListView) findViewById(R.id ...