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. SqlServer中的一些非常用功能

    1.启用双引号作为分隔符 Set Quoted_Identifier on 此时:create table dbo.testcolumn("column" char(2))是合法的 ...

  2. 关于cocoa框架,你所要知道的一切(苹果官方文档,cocoa框架核心竞争力,必须收藏!)

    https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Accessib ...

  3. MongoDB常用操作总结

    ====================================MGDB的操作====================================== 0.创建数据库时使用(use 数据库 ...

  4. 50道JavaScript经典题和解法(JS新手进...持续更新...)

    最近在学习<数据结构与算法JavaScript描述>这本书,对JavaScript的特性和数据结构都有了进一步的了解和体会. 学习之余,也进行了相应的练习,题目难度不大,但是对所学知识的巩 ...

  5. 玩儿了一下django User authentication

    五一在家,VPN不能链接了,而项目在本地run的过程中,又需要链接公司的SSO server才能login.下雨,不想去公司,又不得不在家做task,只能想办法避开SSO login,以前知道djan ...

  6. Arcgis for JS之Cluster聚类分析的实现(基于区域范围的)

    原文:Arcgis for JS之Cluster聚类分析的实现(基于区域范围的) 咱们书接上文,在上文,实现了基于距离的空间聚类的算法实现,在本文,将继续介绍空间聚类之基于区域范围的实现方式,好了,闲 ...

  7. ArcGIS API for Silverlight实现地图测距功能

    原文:ArcGIS API for Silverlight实现地图测距功能 问题:如何实现地图测距功能? 地图工具栏 <Grid x:Name="gToolMenu" Hei ...

  8. 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (二)

    前言 第一篇說明了 TryUpdateModel 的簡單的應用,除了可指定更新的欄位之外也可排除更新特定的欄位,而因為可搭配 Metadata 做欄位驗證為資料又做了一層把關,但在 ASP.NET M ...

  9. Ubuntu-1404 GDB 调试C++报错

    问题 Ubuntu1404下,当用GDB调试C++程序时,报错ImportError: No module named 'libstdcxx' 解决办法 vim ~/.gdbinit #~/.gdbi ...

  10. 配置Tomcat6的管理用户

    在Tomcat上发布war之前需要配置一个管理用户以进入上传页面,而配置就是通过修改/usr/local/tomcat6/conf/tomcat-users.xml这个文件进行的.下面是修改后的文字: ...