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, ... ...
随机推荐
- PHP的环境搭建
下载开发环境 wampserver 下载sublime text 2 sublime使用技巧 1:安装漂亮的编程字体http://pan.baidu.com/s/1xMex9 下载"程序编写 ...
- app微信支付服务器端php demo
class Wxpay { /* 配置参数 */ private $config = array( 'appid' => "wxc92b12277f277355", /*微信 ...
- 高质量程序设计指南C/C++语言——内存管理
• free()和delete只是把指针所指的内容给释放掉,并没有把指针本身删掉.指针被free()或delete以后其地址仍然不变(不等于NULL),只是该地址对应的内存是垃圾——p成了野指针.如果 ...
- mybatis日记配置Log4j
拷贝log4j-1.2.16.jar到项目lib下 方式一是在src下新建一个log4j.xml文件,其具体内容如下 <?xml version="1.0" encoding ...
- inline-block 垂直居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ 1087 互不侵犯King (位运算)
题解:首先,这道题可以用位运算来表示每一行的状态,同八皇后的搜索方法,然后对于限制条件不相互攻击,则只需将新加入的一行左右移动与上一行相&,若是0则互不攻击,方案可行.对于每种方案,则用递推来 ...
- java学习之jdbc的封装
jdbc是连接数据库必不可少的工具,但每次连接都要重新写一遍太麻烦了,也不利于代码的可读性,这里做一个工具类进行封装. package com.gh; import java.sql.Connecti ...
- Android Blur效果之FastBlur
Blur 自从iOS系统引入了Blur效果,也就是所谓的毛玻璃.模糊化效果,磨砂效果,各大系统就開始竞相模仿,这是一个怎样的效果呢,我们现来看一些图: 这些就是典型的Blur效果,在iOS和MIUI中 ...
- HTML5 总结-表单-输入类型
HTML5 Input 类型 HTML5 新的 Input 类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. 本章全面介绍这些新的输入类型: email url nu ...
- 修改SQL Server 排序规则 (未能排它地锁定数据库以执行该操作)
use master go alter database DBName set single_user go alter database DBName COLLATE Chinese_PRC_CI_ ...