暴力求解——素环数 Prime Ring Problem ,UVa 524
Description
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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个数中相加可能是素数的数放入数组中标记,再用一个数组判断这个数字是否用过(即出现过),需注意的是最后一个数要与第一个数的和要也为素数。
程序代码链接:http://paste.ubuntu.com/11953276/
暴力求解——素环数 Prime Ring Problem ,UVa 524的更多相关文章
- Prime Ring Problem UVA - 524
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,...,n int ...
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- HDOJ1016 Prime Ring Problem(DFS深层理解)
Prime Ring Problem 时间限制: 200 ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 2016年11月1日——jQuery源码学习笔记
1.instanceof运算符希望左操作数是一个对象,右操作数标识对象的类.如果左侧的对象是右侧类的实例,则表达式返回true,否则返回false 2.RegExp.exec() 如果 exec() ...
- 关于WINDOWS命令
1. Windows netstat 查看端口.进程占用 netstat -aon — 显示全部进程 2. 查看进程命令 tasklist — 显示全部进程 taskkill — 关闭至少一个系统进 ...
- linux工具问题,tail -f 失效
最近发现一个很奇怪问题: tail -f 不能实时的输出日志
- sql - 获取日期中的年
使用 YEAR函数, 如 --day为rain表中的字段. select YEAR(day) from t_rain
- oracle 的 startup,startup mount,startup nomount之间的区别
startup,startup mount,startup nomount之间的区别 startup nomount选项:(读初始化参数文件,启动实例) startup nomount选项启动实例 ...
- 灵活运用绑定变量---declare匿名块使用绑定变量
declare type cur01 is ref cursor; v_cur cur01; v_match123 varchar2(2000); v ...
- power desinger 学习笔记<四>
Tools <display preferences> <content table> <advanced> Columns 选择放大镜图标 进入窗口 选择要顺序显 ...
- Sqoop import加载HBase过程中,遇到Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-x
在执行hbase sqoop抽取的时候,遇到了一个错误,如下图: 在执行程序的过程中,遇到权限问题很正常,也容易让人防不胜防,有问题就想办法解决,这个是关键. 解决办法如下: 第一步:su hdfs, ...
- hdoj 1686 kmp
题目: Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN Sample Output 1 3 0 代码: #in ...
- 简明Vim练级攻略
原文:酷壳网 vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim P ...