杭电 1016 Prime Ring Problem
来源:互联网 发布:观战数据不可用 编辑:程序博客网 时间:2024/05/29 15:42
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25187 Accepted Submission(s): 11246
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)
注意边界 还有首尾判断是否符合条件
#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#define M 25using namespace std;int vis[M],n,ans[M],t=0;int sum(int a,int b)//判定素数的函数{ int k=0,sum,i; sum=a+b; for(i=2;i<sum;i++) { if(sum%i==0) { k=1; return 0; } } if(!k) return 1;}void dfs(int x){ int i; if(x==n&&sum(ans[0],ans[n-1]))//递归边界 判定首尾是否符合条件 { for(i=0;i<n;i++) { if(i==0) cout<<ans[0]; else cout<<" "<<ans[i]; } cout<<endl; } else { for(i=2;i<=n;i++) { if(!vis[i]&&sum(i,ans[x-1])) { { ans[x]=i; vis[i]=1;//标记用过的数 dfs(x+1); vis[i]=0;//还原用过的数 } } } }}int main(){ int i,j; while(cin>>n) { t++; memset(vis,0,sizeof(vis)); cout<<"Case "<<t<<":"<<endl; vis[1]=1;ans[0]=1; dfs(1); cout<<endl;//每个样例之间有一个空行 } return 0;}
0 0
- 杭电 HDU 1016 Prime Ring Problem
- 杭电1016-Prime Ring Problem
- 杭电 Prime Ring Problem 1016 DFS
- 杭电 1016 Prime Ring Problem
- 杭电OJ 1016:Prime Ring Problem
- 杭电 1016 Prime Ring Problem
- 杭电1016Prime Ring Problem
- 杭电 1016 Prime Ring Problem
- Prime Ring Problem HDU 杭电1016
- 杭电 1016 Prime Ring Problem【DFS】
- 杭电1016 Prime Ring Problem
- hdu 杭电 1016 Prime Ring Problem
- 杭电OJ 1016 Prime Ring Problem
- 杭电 oj 1016 Prime Ring Problem
- 杭电1016 Prime Ring Problem
- 杭电 Prime Ring Problem
- 杭电OJ——1016 Prime Ring Problem
- 杭电OJ题 1016 Prime Ring Problem解题报告
- android 权限添加
- jquery 为 Table 添加样式
- android 禁用组件的方法(setComponentEnabledSetting )
- 如何调通微信支付及微信发货通知接口(Js API)
- Linux下Apache, MySQL和PHP的安装与配置
- 杭电 1016 Prime Ring Problem
- 基础—三元运算符
- ACdream原创群赛(14)の我今天没吃药
- Javascript 异步加载详解(浏览器在javascript的加载方式)
- Spring工具类详解
- 再谈JSON -json定义及数据类型
- 利用事件对象实现线程同步
- Android中dp和px之间进行转换
- Android在布局中动态添加view的两种方法