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 ...
随机推荐
- HttpURLConnection请求接口
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; ...
- 转:CPU与内存的那些事
下面是网上看到的一些关于内存和CPU方面的一些很不错的文章. 整理如下: 转: CPU的等待有多久? 原文标题:What Your Computer Does While You Wait 原文地址: ...
- lftp
linux安装FTP工具 lftp及使用教程 来源:网络 发布时间:2013-05-24 15:21 字体:[大 中 小] 点击2510次 linux下可以直接通过FTP命令进行ftp上传下载,不 ...
- 自己学习smarty的一些代码 和记录
http://www.yiibai.com/smarty/smarty_install.html 手册可以看这里 index.tpl <!DOCTYPE html> <html&g ...
- html 实体 => htmlspecialchars
因为没有办法判断用户输入的数据是否安全, 可能含有和sql语句相冲突的字符,比如 ' " < >,那就有一个办法, 把这些特殊字符处理一下,比如吧空格符号变成 ,这样sql语句 ...
- [转]使用git命令上传代码
http://jiajing.elastos.org/2013/04/15/%E4%BD%BF%E7%94%A8git%E5%91%BD%E4%BB%A4%E4%B8%8A%E4%BC%A0%E4%B ...
- Windows日志查看工具合集
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://github.com/jiangxincode 知乎地址 ...
- C语言中随机数相关问题
用C语言产生随机数重要用到rand函数.srand函数.及宏RAND_MAX(32767),它们均在stdlib.h中进行了声明. int rand(void);//生成一个随机数 voidsrand ...
- hdu 1075 二分搜索
还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...
- robot API笔记2
robot.conf 设计方案 实现设置测试执行和输出处理. 这个方案实现了 RobotSettings 和 RebotSettings 内部使用的类 该框架. 不应该有这些类需要使用外部.这个包可以 ...