A - Prime Ring Problem(素数环,深搜,打表)
Description
Note: the number of first circle should always be 1.
Input
Output
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
8
Sample Output
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
#include <cstdio>
#include <string.h>
#include <math.h>
int num[], t, n, now; //num记录最后选中的数
bool vis[], pri[];//pri[i] = 1表示i为素数,pri[i] = 0表示i不是素数
bool prime(int y) //bool类型的函数,这样就不用定义变量作为返回值了
{
for(int i = ; i <= sqrt(y); i++)
{
if(y % i == )
return false;
}
return true;
}
void dfs(int now, int t) //已经找到t个数了,第t个数为now,也就是当前数为now,进入dfs也就是找第t+1个数
{
if(t == n) //该找第n个数时,看看第n个数和第一个数相加是否是素数,因为组成的是个环,ps:一定记得!!!
{
if(pri[now+])
{
printf("");
for(int i = ; i <= n; i++)
printf(" %d", num[i]);
printf("\n");
}
return;
} for(int i = ; i <= n; i++)
{
if(vis[i] && pri[i+now])
{
vis[i] = ;
num[t+] = i;
dfs(i, t+);
vis[i] = ;
}
} }
int main()
{
int cnt = ;
while(~scanf("%d", &n))
{
memset(vis, , sizeof(vis));
for(int i = ; i < ; i++)
{
if(prime(i))
pri[i] = ;
else
pri[i] = ;
}
printf("Case %d:\n", cnt++); //当时忘了冒号!!!!!!!!!!!!!!!!!!!!!!!!!!惨啊~~~~
num[]=; vis[] = ;
dfs(, );
printf("\n");//当时也忘了每个案例之间也要输出空行~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!嗷~~~
}
return ;
}
A - 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 质数环
Description 输入正整数n,把整数1,2,-,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.n<=16. A ring is com ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- 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, ... ...
随机推荐
- XML读写文件辅助类
/// <summary> /// 历史下载记录xml文件操作 /// </summary> public class XMLHelper { private string x ...
- C++内置类型对象之间的转换
C++定义了一组内置类型对象之间的标准转换,在必要时它们被编译器隐式地应用到对象上. 隐式类型转换发生在下列这些典型情况下. 1. 在混合类型的算数表达式中 规则:在这种情况下最宽的数据类型成为目标转 ...
- 我用过的linux命令--安装Hadoop
1. hadoop软件传送给虚拟机 还是利用WinSCP把hadoop软件安装包,放到linux的Downloads文件夹中. 2. 选择安装目录 把hadoop安装包copy到这个安装目录中,这里我 ...
- IDEA 15 社区版 Maven项目 启动Tomcat调试
1.在pom下添加Tomcat插件: <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifac ...
- XHTML CSS 常见问题和解决方案
原文地址:XHTML CSS 常见问题和解决方案 作为前端开发人员,在日常的页面制作时,不可避免的会碰上这样那样的问题,我挑选了其中的一些进行总结归档,希望对大家会有所帮助: 1.如何定义高度很小的容 ...
- IOS 警告框 (UIAlertView)的使用方法
1.普通警告框 IOS的SDK中提供了一个方便的类库UIAlertView,配合着不同参数来使用此类可以做出大多数的警告框,如下代码是IOS最简单的警告框. UIAlertView *alert = ...
- 升级Xcode6.3插件失效解决办法
1.打开终端,输入以下代码获取到DVTPlugInCompatibilityUUID defaults read /Applications/Xcode.app/Contents/I ...
- Java 多线程 socket 取款例子 runnable callable
socket部分参考 http://blog.csdn.net/kongxx/article/details/7259465 取款部分参考 http://blog.csdn.net/dayday198 ...
- MySQL对于有大量重复数据表的处理方法
需要在MySQL的一张innodb引擎的表(tableA)上添加一个唯一索引(idx_col1_u).但是对于每个key(col1)表中已经有大量重复数据.此时,做数据的手工清理,或者SQL处理是非常 ...
- Python的MySQLdb模块安装,连接,操作,增删改
1. 首先确认python的版本为2.3.4以上,如果不是需要升级python的版本 python -V 检查python版本 2. 安装mysql, 比如安装在/usr/local/my ...