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. 洛谷—— P1051 谁拿了最多奖学金

    https://www.luogu.org/problem/show?pid=1051 题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖 ...

  2. Loj #6307. 「雅礼国庆 2017 Day1」Clique

    link: https://loj.ac/problem/6307 最大团转补图的独立集,这样的话只有r[x]<l[y]或者r[y]<l[x],x和y才能连边,所以排序之后乱搞就行了. 需 ...

  3. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  4. 2.JAVA语言基础部分

    1.语言基础 二进制操作 "&"按位与:a与b同时为1结果为1,否则为0: "|"按位或:a与b其中任一个为1,否则为0 "~"按位 ...

  5. Angular 路由⑦要素

    cnzt       http://www.cnblogs.com/zt-blog/p/7919185.html http://www.cnblogs.com/zt-blog/p/7919185.ht ...

  6. 使用SourceTree 来管理 Gitcafe 的Pages 发布Blog!

    有个好爹的 SourceTree 是来自 JIRA 的娘家 Bitbucket 的新东家 ATLASSIAN.com 一家成功的,对敏捷软件工程拥有全栈式支持的商业公司, 所推出的 MAC 专用, S ...

  7. Mysql 之配置文件my.cnf

    mysql配置文件为my.cnf,它所在位置根据安装时设定的. 当mysqld服务启动的时候,默认会按一定的顺序读取配置文件的. 1 2 3 [root@zhu2 ~]# /opt/mysql/lib ...

  8. 微信小程序 项目实战(一)生命周期 配置服务器信息 splash启动页

    步骤一:小程序 生命周期 //app.js App({ onLaunch: function () { //当小程序初始化完成时,会触发onLaunch(全局只触发一次) }, onShow: fun ...

  9. Cocos2d-x学习资源

    1.CSDN博客:http://blog.csdn.net/legendof1991/article/category/2161091.主要讲的Cocos2d-x3.0 2.CSDN博客:http:/ ...

  10. Deep Learning for Robotics 资源汇总

    1 前言 在最新Nature的Machine Intelligence 中Lecun.Hinton和Bengio三位大牛的Review文章Deep Learning中.最后谈The Future Of ...