UVA - 524:Prime Ring Problem
Prime Ring Problem
来源:UVA
题目
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1, 2,…, n 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.
输入
n (0 < n<=16)
输出
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.
输入样例
6
8
输出样例
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4Case 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>
int prime[]={2,3,5,7,11,13,17,19,23,29,31,37};
int arr[20],vis[20];
int n;bool is_prime(int num)
{for(int i=0;i<12;i++)if(prime[i]==num)return true;return false;
}
void dfs(int cur)
{if(cur==n && is_prime(arr[0]+arr[n-1])){for(int i=0;i<n;i++)printf("%d%c",arr[i],i==n-1?'\n':' ');}else for(int i=2;i<=n;i++){if(!vis[i] && is_prime(i+arr[cur-1])){arr[cur]=i;vis[i]=1;dfs(cur+1);vis[i]=0;}}
}
int main()
{int cnt=0;while(scanf("%d",&n)!=EOF){if(cnt)printf("\n");printf("Case %d:\n",++cnt);arr[0]=1;dfs(1);}return 0;
}
UVA - 524:Prime Ring Problem相关推荐
- UVA - 524 Prime Ring Problem
题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as ...
- HDU1016 Prime Ring Problem dfs+回溯
点击打开链接 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- UVA524 素数环 Prime Ring Problem
UVA524 素数环 Prime Ring Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/UVA52 ...
- HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述: Prime Ring Problem Time Limit: 4000/2000 ...
- [HDOJ1016]Prime Ring Problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
- hdu 1016 Prime Ring Problem(DFS)
本题链接:点击打开链接 本题大意: 输入一个数n表示须要查找的数有n个,而且这些数连成一个环,随意两个相邻的数之和都为素数. 解题思路: 就是从1開始对每一个点进行查找,符合条件的点就存到一个数组中, ...
- HDU-1016 Prime Ring Problem DFS
简单DFS,需要注意的是最后的那个数加上一要是个素数. 代码如下: #include <cstring> #include <cstdlib> #include <cst ...
- 杭电 Prime Ring Problem
解题思路: 题目是一道很典型的dfs搜索题目,思路很清晰,很容易想到.想组成这个素数环,要有n步,而每步都有n-i(i是变动的)种可能,所以利用dfs深搜,并融合回溯法,这一步搜不到的话既返回至上一步 ...
最新文章
- Unencrypted connection refused. Goodbye. Connection closed by foreign host.
- 转载 C++实现的委托机制
- 怎么像编辑提供wb原图_PLoS One:7月1日起,投稿必须提供原始WB图片!
- 在P2P市场中代替“看不见的手”的算法在哪里
- Kestrel的ListenAnyIP和ListenLocalhost的区别
- STM32F1笔记(十二)DAC
- The Stable Marriage Problem(POJ-3487)
- 基于php重庆旅游设计网站作品
- 日常排版--word中的一些小技巧(交叉引用)
- 阿里矢量图标库的使用方法
- 推荐一个好用的百度文库在线免费下载文档网站
- Go语言详解内存对齐
- grep/awk/sed查指定时间段日志
- 【干货】Python文件打包 .exe文件反编译
- springboot配置打印日志文件
- WebLogic Server 9.2 直接下载 地址分享
- 国内程序员的Linux开发平台-Deepin
- 储能双向变流器(PCS)设备中的电流检测方法
- Linux等比数列脚本求和,POJ 1845 (约数和+二分等比数列求和)
- speedoffice表格中如何插入子表
热门文章
- java根据手机号获取微信信息_Java实现的微信公众号获取微信用户信息示例
- 哈弗h5倒车开关在哪_哈弗h5空调吹风方向怎么调-哈弗h5空调开关图示
- android无线充电技术,国外大神发现Android 11隐藏功能:反向无线充电
- 朋友圈怎么发长视频?安利一个解决办法,简单易懂!
- linux usb修复工具下载,Linux助手:Universal USB Installer新版下载
- linux压缩和解压缩命令tar,zip,gzip
- cyq.data mysql_CYQ.Data 轻量数据层之路 V4.3 版本发布[增加对SQLite,MySQL数据库的支持]...
- 关于运维,阿里云、字节、华科的专家如是说
- 硅胶隆胸的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- Android项目实战_手机安全卫士拦截骚扰