「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, ... ...
随机推荐
- luogu P1351 联合权值
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- java实现简单的算法
排序大的分类可以分为两种:内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.下面讲的排序都是属于内排序. 内排序有可以分为以下几类: (1).插 ...
- sprak pom
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...
- Maven的仓库
以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_repositories.html: 什么是Mave ...
- android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)
http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...
- MD5进行文件完整性校验的操作方法
我组产品包含大量音频和图片资源,MD5主要就用来检测这些资源文件的完整性.主要思路是:先计算出所有资源文件的MD5值,存到一个xml文件中,作为标准的MD5值.然后把这个xml文件放到我们的产品中,每 ...
- 仿MIUI音量变化环形进度条实现
Android中使用环形进度条的业务场景事实上蛮多的,比方下载文件的时候使用环形进度条.会给用户眼前一亮的感觉:再比方我大爱的MIUI系统,它的音量进度条就是使用环形进度条,尽显小米"为发烧 ...
- Solidworks工程图如何使用,替换图纸格式模板文件
1 如果你有了图纸模板(SLDDRW文件),比如下面的这个文件,则以后把零件的三维图拖放到里面就可以了.注意,这是最简单,最有效的方法,后面另存为的slddrt文件不好使,所以还不如把模板文件另存为S ...
- 2014牡丹江 现场赛 F zoj 3824 Fiber-optic Network
首先赞一下题目, 好题 题意: Marjar University has decided to upgrade the infrastructure of school intranet by us ...
- php闭包实例
php闭包函数,一个典型的实例 function getMoney() { $rmb = 1; $dollar = 6; $func = function($dollar) use (&$rm ...