HDU 1016 Prime Ring Problem(深搜)
来源:互联网 发布:mysql partition by 编辑:程序博客网 时间:2024/05/16 13:38
#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>using namespace std;int fun1(int a,int b[]){ int flag=1,k; for(k=0;k<a;k++) { if(b[k]==b[a])//出现过了 { flag=0; break; } } return flag;//返回是否出现过}int fun2(int a,int c,int b[]){ int k,flag=1; for(k=2;k<=sqrt(b[a]+b[a-1]);k++) { if((b[a]+b[a-1])%k==0)//判断素数 { flag=0; break; } } if(flag&&(a==c-1)) { for(k=2;k<=sqrt(b[a]+b[0]);k++) { if((b[a]+b[0])%k==0) { flag=0; break; } } } return flag;//返回是否是素数的标记}void fun3(int a,int b_[]){ int k; printf("1 "); for(k=1;k<a-1;k++) printf("%d ",b_[k]); printf("%d\n",b_[k]);}int main(){ int i=1,n,a[20],c_=0,x,m=0; memset(a,1,sizeof(a)); while(scanf("%d",&n)!=EOF) { i=1,c_=0; while(1) { while(1) { a[i]++; x=1; if(a[i]==n+1) { a[i]=1; i--; x=0; } if(x&&fun1(i,a))// { if(fun2(i,n,a)) { if(i==n-1) { if(c_==0) printf("Case %d:\n",++m); fun3(n,a); c_=1; i--; } i++; if(a[0]>=2)break; } if(a[0]>=2)break; } if(a[0]>=2)break; } if(a[0]>=2) { a[0]=1; printf("\n"); break; } } } return 0;}
题目概述:
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.
Simple Input:
6
8
Simple 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
题目分析:给定数n(n<20),在n的所有排列中,记a[1],a]2],...,a[n],求满足a[i]+a[i+1](1<=i<n)是素数并且a[1]+a[n]也是素数的所有排列。排列按字典序输出。
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 1016 Prime Ring Problem(DFS)
- hdu 1016 Prime Ring Problem(回溯)
- hdu 1016 Prime Ring Problem(DFS)
- hdu 1016 Prime Ring Problem(水题)
- HDU 1016 Prime Ring Problem (DFS)
- HDU 1016Prime Ring Problem(dfs)
- hdu 1016 Prime Ring Problem (dfs)
- HDU 1016 Prime Ring Problem(DFS)
- HDU--1016:Prime Ring Problem (DFS)
- hdu 1016 Prime Ring Problem (DFS)
- HDU 1016-Prime Ring Problem(DFS)
- hdu 1016 Prime Ring Problem(dfs)
- iReport将被Jaspersoft Studio所取代
- HDU3974 - Assign the task(线段树)
- ~~~~~不错的
- UVaLive/LA 6806 Hari Merdeka(AC自动机,DP)
- 关于老版ubuntu更新源问题
- HDU 1016 Prime Ring Problem(深搜)
- 使用svn diff的-r参数的来比较任意两个版本的差异
- 在arc下默认关键字
- 黑马笔记4:LogCat简介、ContentProvider和内容观察者、获取图片exif信息
- 遥感图像数据库下载大全
- Visual Studio 之 Background 改变颜色
- Android 双击返回键退出程序 实现
- scala actor
- UFLDL Exercise:Learning color features with Sparse Autoencoders