Description


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

A ring is composed of n (even number) circles as shown in diagram.

Put natural numbers1,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<=16)

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.

You are to write a program that completes above process.

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<=16,数据很小,可以暴搜,途中用stack记录经历的点

这道题的难点是输出格式啊QAQ!!!

example:

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//这里也没有空格
//这里有回车

//luogu的原题中输出格式是崩坏的啊!!!QAQ

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n;
bool sf[21];
int stack[21];
int flag;
bool prime(int x)
{
for(int i=2;i<=sqrt(x);++i)
if(x%i==0)return 0;
return 1;
}
int tot=0;
void print()
{
if(!prime(1+stack[n]))return;
if(!flag){
if(tot)cout<<endl;
flag++;
printf("Case %d:",++tot);
}
cout<<endl;
cout<<1;
for(int i=2;i<=n;++i)
cout<<" "<<stack[i];
}
void search(int x)
{
for(int i=2;i<=n;++i)
if(!sf[i]&&prime(stack[x-1]+i))
{
stack[x]=i;
sf[i]=1;
if(x==n)print();
else search(x+1);
sf[i]=0;
}
return;
}
int main()
{
while(cin>>n)
{
memset(sf,0,sizeof(sf));
flag=0;
stack[1]=1;
sf[1]=1;
search(2);
cout<<endl;
}
}

「UVA524」 Prime Ring Problem 质数环的更多相关文章

  1. Prime Ring Problem素数环(HDU1016)

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

  2. hdu 1016 Prime Ring Problem (素数环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. #include <iost ...

  3. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

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

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

  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. CSS3 伪类选择器 nth-child() 的用法

    伪类选择器 nth-child() 在IE6-8和FF3.0-浏览器不支持,CSS3中nth-of-type(n)(比如nth-of-type(1))这个特殊的类选择符可以样式更加个性的标题和段落等, ...

  2. 让win7任务条上的文件夹打开是c,d,e,f而不是库

    如果资源管理器是打开的,则右键点击资源管理器的图标,在跳出的菜单中,右键点击“Windows资源管理器”,选择“属性”. 在“快捷方式’选项卡,“目标”一栏,默认的是 %windir%\explore ...

  3. 未能加载文件或程序集“System.EnterpriseServices, Version=4.0.0.0或2.0.0.0

    未能加载文件或程序集“System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50 ...

  4. android 播放MP3

    <?xml version="1.0" encoding="utf-8"?> <!-- 定义当前布局的基本LinearLayout --> ...

  5. 【js html】对于<img>图片的引用填充,src可以给什么值?

    平时多见的<img>的使用,常见于如下: <img class="img-responsive img-rounded" src="static/img ...

  6. Python 一行命令ftp服务器

    Obligatory Twisted example: twistd -n ftp And probably useful: twistd ftp --help Usage: twistd [opti ...

  7. springboot mybatis 项目框架源码 shiro 集成代码生成器 ehcache缓存

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.service等完整模块2. ...

  8. Version和Build的差别

    [1]概念 iOS的版本号号.一个叫做Version,一个叫做Build,这两个值都能够在Xcode 中选中target,点击"Summary"后看到. Version在plist ...

  9. HDU 1003 Max Sum (动规)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  10. 备忘录模式-Memento

    备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 备忘录模式结构图: 何时使用备忘录模式: Memento模式比适合 ...