HDU 1016 Prime Ring Problem
来源:互联网 发布:php小数转换为整数 编辑:程序博客网 时间:2024/05/20 23:39
Prime Ring Problem
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 101 Accepted Submission(s) : 21
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 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.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
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. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
68
Sample Output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
Source
Asia 1996, Shanghai (Mainland China)
相对简单的一道题,dfs深搜。即在N个数的全排列,找出符合提议的素数组。
注意:一边排列 一边检查
ps:本人大三狗一枚,正在持续更新博客,文章里有任何问题,希望各位网友可以指出。若有疑问也可在评论区留言,我会尽快回复。希望能与各位网友互相学习,谢谢!
#include<stdio.h>#include<math.h>int num;bool use[20];int CP[20];bool isprime(int a){for(int i=2;i<=sqrt(a+0.0);i++){if(a%i==0)return false;}return true;}void dfs(int n){if(n==num&&isprime(1+CP[n-1])){for(int i=0;i<num;i++){printf(i==num-1?"%d\n":"%d ",CP[i]);}}else{for(int i=2;i<=num;i++){if(!use[i]&&isprime(i+CP[n-1])){CP[n]=i;use[i]=true;dfs(n+1);use[i]=false;}}}}void init(){for(int i=1;i<=num;i++){use[i]=false;}CP[0]=1;use[1]=true;}int main(){int time=0;while(scanf("%d",&num)!=EOF){init();printf("Case %d:\n",++time);dfs(1);puts("");}}
0 0
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 prime ring problem
- hdu 1016Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 Prime Ring Problem
- HDU-1016-Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 - Prime Ring Problem
- HDU 1016 Prime Ring Problem
- 二分图—匈牙利算法
- Java创建文件和文件夹、文件属性的读取、文件属性的设置
- google code jam 2015 D. Ominous Omino
- 随笔----戴上耳机逃离这个世界
- R中进行PCA主成分分析
- HDU 1016 Prime Ring Problem
- MyEclipse10.6 安装SVN插件方法及插件下载地址
- ZigBee智能节水灌溉系统
- 鸟哥的linux私房菜(服务器架设篇)(1)
- C++类型转换函数(类型转换运算符函数)
- frame与frame之间如何用jquery传值
- Binary Tree Right Side View
- mysql语句---从创建到使用
- UITableView学习笔记