HDU1016 Prime Ring Problem
来源:互联网 发布:windows defender红叉 编辑:程序博客网 时间:2024/06/04 19:59
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 48566 Accepted Submission(s): 21406
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)
题意:环中任意相邻两数之和为素数,并且第一个数一直是1
思路:这道题可以用深搜递归写,当然暴力也可以(只是不知道会不会超时)
这里介绍用递归的解法:
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>using namespace std;int a[25];int vis[25];int n;int judge(int m){ int i,j; int c=sqrt(m); for(i=2;i<=c;i++) if(m%i==0) break; if(i<=c) return 1; return 0;}void dfs(int num){ int i=1; if(num==n&&judge(a[0]+a[n-1])==0)//第一位和最后一位也相邻,要判断一下是否和为素数 { for(i=0;i<n-1;i++) printf("%d ",a[i]); printf("%d\n",a[n-1]); } else { for(i=2;i<=n;i++)//递归构造全排列 { if(judge(i+a[num-1])==0&&vis[i]==0) { a[num++]=i; vis[i]=1; dfs(num);//继续下一位 vis[i]=0; num--; } } }}int main(){ int t=0; while(cin>>n) { t++; printf("Case %d:\n",t); memset(vis,0,sizeof(vis)); a[0]=1;//第一位有特殊要求 dfs(1); cout<<endl; } return 0;}
这道题在南阳理工后来又提交总是Timelimitexceeded.就觉得递归没问题,只要在开始之前判断一下奇偶就可以了。奇数肯定是没有满足条件的。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<math.h>#include<algorithm>using namespace std;int vis[20];int num[20];int ans=0;int prime(int n){ int i; if(n==0||n==1) return -1; if(n==2||n==3) return 1; for(i=2;i<=sqrt(n);i++) { if(n%i==0) { return -1; } } return 1;}void dfs(int n,int cur,int a[]){ if(cur==n) { if(prime(1+a[n-1])==1) { cout<<"1"; for(int i=1;i<n;i++) cout<<" "<<a[i]; cout<<endl; ans++; } return ; } for(int i=2;i<=n;i++) { if(vis[i]==0&&prime(a[cur-1]+i)==1) { vis[i]=1; a[cur]=i; dfs(n,cur+1,a); vis[i]=0; } }}int main(){ int n,i,a[20],t=0,flag=0; while(cin>>n,n!=0) { t++; cout<<"Case "<<t<<":"<<endl; if(n%2&&n!=1) { printf("No Answer\n"); continue; } memset(vis,0,sizeof(vis)); a[0]=1; vis[0]=1; dfs(n,1,a); cout<<endl; } return 0;}
0 0
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- hdu1016-Prime Ring Problem
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- HDU1016-Prime Ring Problem
- HDU1016 Prime Ring Problem
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- HDU1016 Prime Ring Problem
- [HDU1016]Prime Ring Problem
- hdu1016 Prime Ring Problem
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- hdu1016:Prime Ring Problem
- hdu1016 Prime Ring Problem
- STL之set的基本用法
- 【JZOJ5043】【NOI2017模拟4.4】保持平衡
- ubuntu下安装flash
- 简单的jquery拖拽排序效果(转载)
- OpenCV检测篇(二)——笑脸检测
- HDU1016 Prime Ring Problem
- android studio升级至2.3后运行app,在手机上没有自己启动
- A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束
- 【敏捷开发每日一贴】:猪和鸡的故事
- [BBED]Oracle 11.2.0.4 Centos6.5下编译bbed
- windows下数据库的创建方法汇总
- 最全面总结 Android WebView与 JS 的交互方式
- 闭包总结
- 递归算法#JDK中主要包#使用javadoc生成自己的API文档