题目OJ地址:

https://www.luogu.org/problemnew/show/UVA524

hdu oj 1016:  https://vjudge.net/problem/HDU-1016

zoj 1457  :https://vjudge.net/problem/ZOJ-1457

题意翻译

输入正整数n,把整数1,2,...,n组成一个环,使得相邻两个整数之和均为素数。输出时,从整数1开始逆时针排列。同一个环恰好输出一次。.

多组数据,读入到EOF结束。

第i组数据输出前加上一行Case i:

相邻两组数据中间加上一个空行。

Prime Ring Problem

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.

Inputn (0 < n < 20). 
OutputThe 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
 #include<stdio.h>
#include<stdlib.h>
#include<math.h>
int N;
int b[]={};
int total=,a[]={};
int search(int); //回溯过程
int print(); //输出方案
int pd(int x,int y); //判断素数x+y是否质数 int main()
{
int t=;
while(scanf("%d",&N)!=EOF)
{
a[]=;
for(int i=;i<;i++) a[i]=;
for(int i=;i<;i++) b[i]=;
t++;
printf("Case %d:\n",t);
search();
printf("\n");
}
//printf("%d\n",total); //输出总方案数
}
int search(int t)
{
int i;
for(i=;i<=N;i++) //有20个数可选
if((!b[i])&&pd(a[t-],i)) //判断与前一个数是否构成素数及该数是否可用
{
a[t]=i;
b[i]=;
if (t==N) { if(pd(a[N],a[])==) print();}
else search(t+);
b[i]=;
}
}
int print()
{
int j;
total++;
//printf("<%d>",total);
printf("%d",a[]);
for(j=;j<=N;j++)
printf(" %d",a[j]);
printf("\n");
}
int pd(int x,int y)
{
int k=,i=x+y;
while(k<=sqrt(i)&&i%k!=) k++;
if(k>sqrt(i)) return ;
else return ;
}

本题目分析见  https://www.cnblogs.com/huashanqingzhu/p/4747009.html

这里要注意:洛谷OJ的测试数据比较弱,n最大是16.  hdu oj和uva oj原题的n是到20的。

UVA524 素数环 Prime Ring Problem的更多相关文章

  1. 洛谷UVA524 素数环 Prime Ring Problem

    标签:搜索与回溯 题目: 从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数. 算法分析: 非常明显,这是一道回溯的题目.从1开始,每个空位有20种可能,只要填进去的数合法:与前面的数不相 ...

  2. 素数环 Primg Ring Problem

    素数环 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/C 题意: 输入正整数n,把整数1~n组成一个 ...

  3. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  4. Prime Ring Problem素数环(HDU1016)

    Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...

  5. uva 524 prime ring problem——yhx

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

  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. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

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

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

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

  9. Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

随机推荐

  1. 如何让你的数据有null

    2018-11-13   09:25:17 如何让你的数据有null 返回时null属性不显示: String str = JSONObject.toJSONString(obj); 返回为null属 ...

  2. 富文本编辑器上传图片需要配置js,后台代码

    富文本编辑器上传图片需要配置js,后台代码

  3. Idea中在代码顶部添加自定义作者和时间

    一.在工具栏找到File=>settings=>Editor=>File and Code Templates 二.选择右边的File Header,在编辑框中添加自定义信息,例如: ...

  4. DDoS攻击与防御(1)

    分布式拒绝服务攻击的精髓是,利用分布式的客户端,向服务提供者发起大量看似合法的请求,消耗或长期占用大量资源,从而达到拒绝服务的目的.从不同的角度看,分布式拒绝服务攻击的方法有不同的分类标准.依据消耗目 ...

  5. python实现简单工厂模式

    python实现简单工厂模式 模式定义 简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式.在简单工 ...

  6. Farewell Party-构造

    Farewell Party 思路 : 转换思路 ,有 a [ i ] 个不相等的 ,那么至少得有 n - a [ i ]个与它相等的. 但是有可能与它拥有相同数目的有很多. 但是为了能够最终 分配成 ...

  7. react 15.5版本的警告问题

    一.问题描述 前几天从git上拉下一个项目,cnpm i 安装依赖,项目跑起来后,发现有两个警告.之前这个项目都好好的,怎么突然报错了? Warning: Accessing PropTypes vi ...

  8. Java并发编程(一)-- 多线程的基本概念

    多线程发展进程 在过去单CPU时代,单任务在一个时间点只能执行单一程序:发展到多任务阶段,计算机能在同一时间点并行执行多任务或多进程--虽然并不是真正意义上的“同一时间点”,而是多个任务或进程共享一个 ...

  9. [HNOI/AHOI2018]道路

    Description: W 国的交通呈一棵树的形状.W 国一共有\(n - 1\)个城市和\(n\)个乡村,其中城市从\(1\)到\(n - 1\) 编号,乡村从\(1\)到\(n\)编号,且\(1 ...

  10. Moo University - Financial Aid [POJ2010] [堆]

    题意: 在C头牛里选N头牛,每头牛需要花掉一定经费ai才能得到一定得bi分,在不超过经费F的情况下,使得N头牛的得分中位数最大.(1 <= N <= 19,999,奇数) (N <= ...