uva 524 prime ring problem——yhx
2016-05-16 10:04
501 查看
Prime Ring Problem |
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 <= 16)
Output
The output format is shown as sample below. Each row represents a seriesof circle numbers in the
ring beginning from 1 clockwisely and anticlockwisely. The order of numbers
must satisfy the above requirements.
You are to write a program that completes above process.
1 #include<cstdio> 2 #include<cstring> 3 bool prm[35],vis[20]; 4 int a[20],n; 5 bool ck(int x) 6 { 7 int i; 8 for (i=2;i*i<=x;i++) 9 if (x%i==0) return 0; 10 return 1; 11 } 12 void dfs(int p) 13 { 14 int i,j,k,x,y,z; 15 if (p==n+1) 16 { 17 if (prm[a +a[1]]) 18 { 19 printf("%d",a[1]); 20 for (i=2;i<=n;i++) 21 printf(" %d",a[i]); 22 printf("\n"); 23 } 24 return; 25 } 26 for (i=2;i<=n;i++) 27 if (vis[i]==0&&prm[i+a[p-1]]) 28 { 29 a[p]=i; 30 vis[i]=1; 31 dfs(p+1); 32 vis[i]=0; 33 } 34 } 35 int main() 36 { 37 int i,j,k,p,q,x,y,z,t; 38 bool bbb=0; 39 for (i=2;i<=35;i++) 40 prm[i]=ck(i); 41 a[1]=1; 42 t=0; 43 while (scanf("%d",&n)==1) 44 { 45 if (bbb) printf("\n"); 46 bbb=1; 47 memset(vis,0,sizeof(vis)); 48 vis[1]=1; 49 printf("Case %d:\n",++t); 50 dfs(2); 51 } 52 }
素数环。注意边界。注意每组数据间的回车(虽然题上没说)。
相关文章推荐
- 用 Retrofit 2 简化 HTTP 请求
- 提升mac下的开发效率
- 二分查找(折半查找) -- 已排序序列的查找算法
- 稀疏矩阵
- 发布Mvc 项目 mvc.dll版本号不一致 出现 3.0.0.1 和4.0.0.1的解决方案
- iOS模型设计---字典转模型
- Contains Duplicate II
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- spring经常找不到xsd
- Ubuntu下编写一个简单的c程序
- 操作数据库的WebService,传递ResultSet的方法
- CTCC2016中国云计算技术大会参会感想
- iOS8下,cell分割线右移
- 阻止表单提交按钮多次提交的完美解决方法
- HTML table的td内容过长时,自动隐藏效果
- C#和.NET框架和术语
- uva 572 oil deposits——yhx
- 通过Service Builder改变了原数据库的主键,所有原有记录被删除。
- 【Java】类执行过程
- HashSet vs TreeSet vs LinkedHashSet