nyoj 素数环
算法:搜索
描述
有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环。
为了简便起见,我们规定每个素数环都从1开始。例如,下图就是6的一个素数环。
输入有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束。输出每组第一行输出对应的Case序号,从1开始。
如果存在满足题意叙述的素数环,从小到大输出。
否则输出No Answer。样例输入6
8
3
0
样例输出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
Case 3:
No Answer
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <stdio.h>
using namespace std;
int a[23],b[23],n,c[50],p;
void cmp()
{ memset(c,1,sizeof(c));
int i,j,k;
for(i=2;i<20;i++)
{
for(j=i*2;j<39;j=j+i)
c[j]=0;
}
c[0]=c[1]=0;
}
int dfs(int n,int k)
{
if(k==n&&c[b[0]+b[n-1]])
{
p=1;
for(int j=0;j<n;j++)
{
if(j) cout<<" ";
cout<<b[j];
}
cout<<endl;
}
else
{
for(int i=2;i<=n;i++)
{
if(!a[i]&&c[b[k-1]+i])
{
a[i]=1;
b[k]=i;
dfs(n,k+1);
a[i]=0;
}
}
}
}
int main()
{ int i,j=0,k;
while(scanf("%d",&n)&&n)
{ cmp();
p=0;
memset(a,0,sizeof(a));
b[0]=1;
printf("Case %d:\n",++j);
if(n%2==0||n==1)dfs(n,1);
if(p==0) printf("No Answer\n");
}
}
nyoj 素数环的更多相关文章
- nyoj素数环
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
- nyoj 488 素数环
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
- ny488 素数环
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起 ...
- DFS搜索题素数环
素数环: 输入整数1,2,3,4,5,···,n组成一个环,使得相邻两个整数之和均为素数. 输出时从整数1开始逆时针排列.同一个环应恰好输出一次.n<=16. Sample: input: 6 ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- ACM_素数环(dfs)
Problem Description: 如图所示,环由n个圆组成. 将自然数1,2,...,n分别放入每个圆中,并且两个相邻圆中的数字总和应为素数. 注意:第一个圆圈的数量应该始终为1. Input ...
- EOJ 1114 素数环
题意 一个由自然数 1…n (n≤18) 素数环就是如下图所示,环上任意两个节点上数值之和为素数. 1 / \ 4 2 \ / 3 Input 输入只有一个数 n,表示你需要建立一个 1… ...
- HDU 1016 素数环(dfs + 回溯)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 一道很典型的dfs+回溯: 根据题意首先进行初始化,即第一个位置为1,然后进行dfs, ...
- 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem
/* Name: NYOJ--488--素数环 Author: shen_渊 Date: 15/04/17 15:30 Description: DFS,素数打个表,37以内就够用了 */ #incl ...
随机推荐
- 你好,C++(1)C++是什么?C++的“前世今生”
The world is built on C++. ——Herb Sutter the chairman of the ISO C++ standards committee and chief n ...
- django TypeError: 'module' object is not callable
原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...
- jQuery自学笔记(三):jQuery动画效果
jQuery隐藏和显示: 使用 hide( ) 和 show( ) 方法来隐藏和显示 HTML 元素: 语法: $(selector).hide(speed,callback); $(selector ...
- 【Node.js】Event Loop执行顺序详解
本文基于node 0.10.22版本 关于EventLoop是什么,请看阮老师写的什么是EventLoop 本文讲述的是EventLoop中的执行顺序(着重讲setImmediate, setTime ...
- 使用PowerDesigner 15对现有数据库进行反向工程(图解教程)
1.启动PD 2.菜单:File->Reverse Engineer ->Database 出来New Physical Data Model对话框 点击“确定”按钮,弹出Database ...
- quartz spring
简单例子可参考 http://yangpanwww.iteye.com/blog/797563 http://liuzidong.iteye.com/blog/1118992 关于时间配置可参考另一篇 ...
- centos卸载自带的apache(httpd)
.[root@localhost etc]# rpm -qa|grep httpd,查看与httpd相关软件包. httpd--.el5_2.CentOS. .然后删除httpd: [root@loc ...
- 必须声明标量变量 "@cid"。
出错代码: public bool Delete_List(int cID) { StringBuilder strSql = new StringBuilder(); strSql.Append(& ...
- 用js实现两个select下拉框之间的元素互相移动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 一个poi操作实现导出功能的类
public class ExportExcel<T> { public void exportExcel(Collection<T> dataset, OutputStrea ...