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. v2.0

    #include <stdio.h>#include <string.h>#include <ctype.h>typedef struct node{ char l ...

  2. php mongodb类

    class HMongodb { private $mongo;    //Mongodb连接    private $curr_db_name;    private $curr_table_nam ...

  3. 添加事件(jquery)

    对盒子内部的盒子添加跟本身盒子相同的事件的时候,需要小心谨慎一点. 诸如: 从表象上看似乎没有什么太大问题,但是却存在一个致命的问题,就是每次点击box的时候,都会给test添加一个点击事件,而添加的 ...

  4. 用facebook账号登陆到你的Magento网店

    Inchoo提供magento和facebook连接的扩展,可以到http://inchoo.net/ecommerce/magento/facebook-connect-magento-extens ...

  5. sql server 2008查询窗口怎么显示行数

    工具->选项

  6. sql server 2008语句中的go有什么用?

    GO表示一个批处理的结束, SQLSERVER遇到Go以后就会将GO之前的语句作为一整批进行处理你在SSMS里执行的时候, 通常加不加都可以,但是如果实在SQLCMD下执行, GO就是一个执行命令了另 ...

  7. 开发报表时将已有User做成下拉列表,第一项为label为ALL,value为null

    SELECT 'All' AS LABLE_NAME, NULL AS USER_NAMEUNION ALLSELECT USER_NAME AS LABLE_NAME, USER_NAME from ...

  8. SQLSERVER 数据库查看各表的记录数

    select   a.name as 表名,max(b.rows) as 记录条数   from   sysobjects   a   ,sysindexes   b       where   a. ...

  9. 蒋鑫:为什么 Git 比 SVN 好

    在版本控制系统的选型上,是选择Git还是SVN? 对于开源项目来说这不算问题.使用Git极大地提高了开发效率.扩大了开源项目的参与度. 增强了版本控制系统的安全性,选择Git早已是大势所趋. 但对于企 ...

  10. Flask服务入门案例

    安装 pip install Flask 入门例子 from flask import Flask app = Flask(__name__) @app.route('/hello.world') d ...