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. HTTP请求的缓存(Cache)机制

    原文地址:http://small.aiweimeng.top/index.php/archives/58.html 先来一张图: ####下面简单的来描述一下HTTP Cache机制: 当资源资源第 ...

  2. Ryuji doesn't want to study (树状数组)

    [传送门]:https://nanti.jisuanke.com/t/31460 [题意]给定一个数组a[N],有两种操作, 操作1,给定 l , r,  查询a[l]×L+a[l+1]×(L−1)+ ...

  3. ansible、zabbix、tcpdump

    Ansible 源码安装 https://blog.csdn.net/williamfan21c/article/details/53439307 Ansible安装过程中常遇到的错误 http:// ...

  4. METEOR_PACKAGE_DIRS 无效

    windows中设置METEOR_PACKAGE_DIRS不起作用,一直提示找不到PACKAGES的原因. METEOR_PACKAGE_DIRS设置的路径太长了. 在系统属性 -->高级--& ...

  5. 一道题目- Find the smallest range that includes at least one number from each of the k lists

    You have k lists of sorted integers. Find the smallest range that includes at least one number from ...

  6. 跟着实例学习设计模式(7)-原型模式prototype(创建型)

    原型模式是创建型模式. 设计意图:用原型实例指定创建对象的类型,并通过拷贝这个原型来创建新的对象. 我们使用构建简历的样例的类图来说明原型模式. 类图: 原型模式主要用于对象的复制.它的核心是就是类图 ...

  7. Solidworks如何创建投影曲线

    画好草图之后(草图是在上视基准面上画的)然后点击曲线,投影曲线   面选择要投影的曲面,然后就得到了平面曲线在曲面上的投影得到的空间曲线   注意这种方法对于开环轮廓也是可以用的,比如下面,我定义一个 ...

  8. 入手Arduino Yun,配合Blynk搞一波事情

    前言 原创文章,转载引用务必注明链接. 最近在闲鱼上与别人用RPi2尸体+好的BBB换了个Arduino Yun,等了几天赶在节前收到了.出奇的轻巧,产地台湾,官方正品做工就是精细~采用5v Micr ...

  9. JavaScript - 正则表达式解惑

    正则表达式手册: http://tool.oschina.net/uploads/apidocs/jquery/regexp.html 正则表达式测试地址: http://tool.chinaz.co ...

  10. 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...