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.



Ring Problem" title="Prime Ring Problem">
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;让你写出一个素数环,要求相邻两个数(顺逆时针)和是素数,输出的时候第一个永远是一;
解题思路:按照递归求全排列的思想,从第2个开始递归一直到最后一位,并且首尾也是素数才算是搜索完成;
感悟:一开始我还以为得剪纸,但是只有20个数,一次就过了;
代码:
#include

#include

#include

#include

#define maxn 25

using namespace std;

int ans[maxn],visit[maxn],n;

int Prime(int a)

{

    for(int
i=2;i<=sqrt(a);i++)

       
if(a%i==0)

           
return 0;

    return
1;

}



void dfs(int cur)

{

   
if(cur==n&&Prime(ans[0]+ans[n-1]))//递归到最后一位,并且首尾也能是素数

    {

       
for(int i=0;i

           
printf("%d ",ans[i]);

       
printf("%d\n",ans[n-1]);

    }

    else

    {

       
for(int i=2;i<=n;i++)

       
{

           
if(!visit[i]&&Prime(i+ans[cur-1]))//这个数没用过并且相邻的是素数

           
{

               
ans[cur]=i;

               
visit[i]=1;

               
dfs(cur+1);

               
visit[i]=0;

           
}

       
}

    }

}

int main()

{

   
//freopen("in.txt", "r", stdin);

    int
s=1;

   
memset(visit,0,sizeof(visit));

   
while(~scanf("%d",&n)&&n)

    {

       
for(int i=0;i

           
ans[i]=i+1;

       
printf("Case %d:\n",s++);

       
dfs(1);

       
printf("\n");

    }

}

Prime Ring Problem的更多相关文章

  1. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  2. hdu 1016 Prime Ring Problem(DFS)

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

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

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

  4. 杭电oj 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(深度优先搜索)

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

  6. HDU1016 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 (DFS)

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

  8. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

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

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

  10. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

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

随机推荐

  1. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  2. dynamics 365 AI 解决方案 —— 微软布局

    核心提示:微软在 Office365.Azure 云.Dynamics365 上进行人工智能技术的部署,野心不小. 微软在2016年9月宣布组建自己的 AI 研究小组.该小组汇集了超过 5000 名计 ...

  3. Nginx学习——Nginx启动、停止、重启和信号控制以及平滑升级

    1.Nginx 启动与停止 (1)启动方式 启动格式:Nginx可执行文件地址 -c Nginx配置文件地址 /etc/local/nginx/sbin/nginx -c /root/dufy/ngi ...

  4. mysql操作sql的小技巧

    本篇集中整理一下执行sql的小技巧,这种方式不仅带来了操作上的便捷,也可以保证数据可以数据的安全性. 1:查询数据(保证查询性能) 首先想先解释一下 SELECT * 和 SELECT t.id , ...

  5. 第6章 Overlapped I/O, 在你身后变戏法 ---1

    这一章描述如何使用 overlapped I/O(也就是 asynchronous I/O).某些时候 overlapped I/O 可以取代多线程的功用.然而,overlapped I/O 加上co ...

  6. ObjectSNMP

    下面的例子,就是使用ObjectSNMP获取RFC1213-MIB的例子:其中的system和ifTable对象就是对应的SNMPMIB中的system组合interface中的ifTable表. p ...

  7. 使用jsonp完美解决跨域问题

    调用web接口,get请求,发现提示:No 'Access-Control-Allow-Origin' header is present on the requested resource. 这个和 ...

  8. 不同Activity之间传递线程

    场景:Android由Activiy A创建Activiy B时 ,A创建的线程B中依然需要调用,这时候需要在两个activity之间传递线程的信息. 解决: 方式一:线程自己维护自己的静态句柄(比较 ...

  9. Java 浅拷贝和深拷贝的理解和实现方式

    Java中的对象拷贝(Object Copy)指的是将一个对象的所有属性(成员变量)拷贝到另一个有着相同类类型的对象中去.举例说明:比如,对象A和对象B都属于类S,具有属性a和b.那么对对象A进行拷贝 ...

  10. BZOJ-4915-简单的数字题

    Description 对任意的四个不同的正整数组成的集合A={a_1,a_2,a_3,a_4 },记S_A=a_1+a_2+a_3+a_4,设n_A是满足a_i+a_j (1 ≤i<j≤4)| ...