Program C 暴力求解
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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int is_prime(int n)
{
for(int k= 2; k*k <= n; k++)
if(n% k== 0) return 0;
return 1;
} int n, A[50], isp[50], vis[50];
void dfs(int cur)
{
if(cur == n && isp[A[0]+A[n-1]])
{
for(int i = 0; i < n; i++)
{
if(i != 0)
printf(" ");
printf("%d", A[i]);
}
printf("\n");
}
else
for(int i = 2; i <= n; i++)
if(!vis[i] && isp[i+A[cur-1]])
{
A[cur] = i;
vis[i] = 1;
dfs(cur+1);
vis[i] = 0;
}
} int main()
{
int kase = 0;
while(scanf("%d", &n) == 1 && n > 0)
{
if(kase > 0)
printf("\n");
printf("Case %d:\n", ++kase);
for(int i = 2; i <= n*2; i++)
isp[i] = is_prime(i);
memset(vis, 0, sizeof(vis));
A[0] = 1;
dfs(1);
}
return 0;
}
Program C 暴力求解的更多相关文章
- Program A - 暴力求解
Description Write a program that finds and displays all pairs of 5-digit numbers that between them ...
- Program L 暴力求解
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- Program B 暴力求解
Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maxim ...
- POJ 1562(L - 暴力求解、DFS)
油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...
- 逆向暴力求解 538.D Weird Chess
11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...
- 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型
先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...
- BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~
jrMz and angle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu6570Wave (暴力求解)
Problem Description Avin is studying series. A series is called "wave" if the following co ...
- <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?
str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n) , 暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...
随机推荐
- C#_拆箱跟装箱
Net的类型分为两种,一种是值类型,另一种是引用类型.这两个类型的本质区别,值类型数据是分配在栈中,而引用类型数据分配在堆上.那么如果要把一个值类型数据放到堆上,就需要装箱操作:反之,把一个放在堆上的 ...
- CSS3学习笔记之属性值
font-family 设置文本的字体名称. font-style 设置文本样式. 取值 normal不使用斜体. italic使用斜体. oblique使用倾斜体. inherit从父元素继承. f ...
- 不能上传文件 unknown error happened
做一件事情: 1 找出问题,发现问题,2 搜索问题,3 找到答案,验证之.4 找不到答案,想出思路,验证....5 不断想,记录笔记.
- jQuery Ajax学习
地址:http://www.w3school.com.cn/jquery/jquery_ref_ajax.asp
- js 继承inheritance/extends
主要就是<javascript语言精粹>语言精粹中的内容 5.1伪类 Function.prototype.method = function(name,func){ this.proto ...
- struts2 拦截器的注册在strut.xml中
根据需要:需要在struts.xml中注册拦截器为以下内容 根据自己的业务需要更改参数 <!DOCTYPE struts PUBLIC "-//Apache Software Fou ...
- UB单修改
FUNCTION Z_SD_UB_CHANGE. *"-------------------------------------------------------------------- ...
- abap注意
1.建表的时候所有的数据元素的总长度不能超过1024. 2.表的主键修改在se11激活不成功,但是可以在se11保存,然后到se14中激活. 3.SM12解锁,在很多时候,经常出现某个表或者可修改的地 ...
- 今天同事给介绍了一个LINQ的工具,LINQPad
今天刚知道LINQPad,详细信息参照http://www.linqpad.net/,免费下载,安装之后样子如下所示,根据向导,链接上本地数据库,比较熟悉的操作风格. 对LINQ的了解太浅,还没有更多 ...
- das,nas,san区别——大型数据中心会用NAS+SAN软硬结合思路
DAS(Direct Access Storage-直接连接存储)是指将存储设备通过SCSI接口或光纤通道直接连接到一台计算机上.NAS(Network Attached Storage网络连接存储) ...