hdu 1016 Prime Ring Problem
2016-01-29 15:06
225 查看
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.
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.
Sample Input
Sample Output
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.
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.
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<iostream> #include<string> #include<algorithm> #include<cstdio> #include<cmath> #include<cstring> using namespace std; bool flag[21]={0}; int ans[21]={1}; int isprime[]={3,5,7,11,13,17,19,23,29,31,37}; bool find_p(int n) { for(int i=0;i<11;++i) { if(n==isprime[i]) return true; } return false; } void dfs(int n,int i) { if(i==n&&find_p(ans[0]+ans[n-1])) { cout<<ans[0]; for(int i=1;i<n;++i) cout<<" "<<ans[i]; cout<<endl; } else { for(int j=2;j<=n;++j) { if(flag[j]!=1&&find_p(j+ans[i-1])) { ans[i]=j; flag[j]=1; dfs(n,i+1); flag[j]=0; } } } } int main() { int n,kase=0; while(scanf("%d",&n)!=EOF) { ++kase; memset(flag,0,sizeof(flag)); printf("Case %d:\n",kase); dfs(n,1); cout<<endl; } return 0; }
相关文章推荐
- 关于MyBatis通用Dao的实现
- 附加/分离存储的源代码分析
- oracle合理的使用索引原则
- Oracle 表类型变量的使用
- 十三、中国特色社会主义实质是全体人民普遍幸福主义
- strange problems while you are using Intellij IDEA
- ngrok 反向代理
- Android百度定位导航之基于百度地图移动获取位置和自动定位
- JBoss7.1.1+Spring4+Hibernate4分布式系统的配置
- 构建高性能web
- Android Recipes笔记
- java websocket
- Facebook存储技术方案:找出“暖性BLOB”数据
- 如何执行oracle存储过程,就exec一下?
- SSIS故障排除
- Azure上的MongoDB:如何选择正确的实例类型?
- Azure上的MongoDB:如何选择正确的实例类型?
- CListCtrl 虚拟列表技术
- TProfiler部署文档--笔记
- spring项目执行dao.update等更新操作失败