hdu 1016Prime Ring Problem
来源:互联网 发布:java内存溢出分析工具 编辑:程序博客网 时间:2024/05/02 01:21
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47348 Accepted Submission(s): 20921
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#include<iostream>#include<string>#include<memory.h>#include<algorithm>using namespace std;int primer[40]={0,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0};int n,k=1,a[100],sum;int visited[100];void dfs(int m){if(m==n&&primer[a[m-1]+a[0]]==1)//找到最后一个数且首尾和为素数{ for(int i=0;i<n-1;i++) cout<<a[i]<<" "; cout<<a[n-1]<<endl; } else//没有找到最后一个 { for(int i=2;i<=n;i++)//从2到n-1找{ if(visited[i]==0&&primer[i+a[m-1]]==1)//没有被访问过且和前一个的和为素数 { visited[i]=1;//标记访问过a[m++]=i;//存入数据方便下次输出dfs(m);//递归找visited[i]=0;//返回2重新找 m--;//下次寻找 } } }}int main() {while(cin>>n){ memset(visited,0,sizeof(visited));//标记状态 cout<<"Case "<<k<<":"<<endl; k++; a[0]=1;//数组保存一个素数环 dfs(1);//从1开始搜 (必须从1开始) cout<<endl;}return 0;}
1 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
- 深入浅出MySQL(6)-常用数据存储引擎初探
- C#的post请求
- 高精度减法
- Unity3d DLL脚本通用解密方法
- js代码判断浏览器类型(chrom\IE\firfox\safari\)opera)的类型
- hdu 1016Prime Ring Problem
- C++ / QT 对象序列化(Object Serialization)的实现
- PG和Oracle的差异
- 【转载】Best Practices for Speeding Up Your Web Site
- Web 通信 之 长连接、长轮询(long polling)
- 算法竞赛入门经典 第二版 习题5-2 Ducci序列 Ducci Sequence uva1594
- OpenMP并行程序设计之常用子句使用介绍(二)
- 算法学习-连续子数组求和最大值
- 优化流程分析