「UVA524」 Prime Ring Problem 质数环
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 质数环的更多相关文章
- Prime Ring Problem素数环(HDU1016)
Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...
- hdu 1016 Prime Ring Problem (素数环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. #include <iost ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
随机推荐
- 教妹学 Java:大有可为的集合
00.故事的起源 “二哥,上一篇<泛型>的反响效果怎么样啊?”三妹对她提议的<教妹学 Java>专栏很是关心. “有人评论说,‘二哥你敲代码都敲出幻想了啊.’” “呵呵,这句话 ...
- Python3 MySQL 数据库连接 - PyMySQL 驱动 笔记
sql插入语句(推荐): str_mac = "nihao" # SQL 插入语句 sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ L ...
- 《Java虚拟机原理图解》 1.1、class文件基本组织结构
作为Java程序猿,我们知道,我们写好的.java 源代码,最后会被Java编译器编译成后缀为.class的文件,该类型的文件是由字节组成的文件,又叫字节码文件.那么,class字节码文件里面到底是有 ...
- IIS Express 的怪毛病 vs2013本机调试
本机调试时,如果同一个项目有多个版本,同时debug,可能会串项目调试,造成不必要的困扰: 通常情况下是 IIS express的映射出现了问题: 解决方案: 1.打开目录:查看文件C:\Users\ ...
- ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗
1.ClassLoader Java中类加载出现在哪个阶段,编译期和运行期? 类加载和类装载是一样的吗? :当然是运行期间啊,我自己有个理解误区,改正后如下:编译期间编译器是不去加载类的,只负责编译而 ...
- 深入GCD(二): 多核心的性能
概念为了在单一进程中充分发挥多核的优势,我们有必要使用多线程技术(我们没必要去提多进程,这玩意儿和GCD没关系).在低层,GCD全局dispatch queue仅仅是工作线程池的抽象.这些队列中的Bl ...
- iOS开发 清除电话号码中的其他符号
最近从通讯录读取电话号码,读出得号码如:134-1814-****. 而我需要的为11位纯数字,一直找方法解决此问题,今天终于找到了.. 分享一下…… 代码如下: NSString *original ...
- 常见的哈希Hash算法 & MD5 & 对称非对称加密 & 海明码
参考 Link 另外,这篇文章也提到了利用Hash碰撞而产生DOS攻击的案例: http://www.cnblogs.com/charlesblc/p/5990475.html DJB的算法实现核心是 ...
- 基于源码学习-fighting
今天逛着逛着,看到个培训网站,点进去和客服人员聊了一下.接着,看了看他们的培训课程,想了解一下 嵌入式开发的. (人就是要放空自己,把自己当做什么都不会,当着个婴儿[小学生]一般认真,要学什么知识就是 ...
- cin,和几个get函数的用法
1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...