博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电1016--Prime Ring Problem(Dfs)
阅读量:5965 次
发布时间:2019-06-19

本文共 2406 字,大约阅读时间需要 8 分钟。

Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 34108    Accepted Submission(s): 15098

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.
 

 

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.
 

 

Sample Input
6
8
 

 

Sample 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
 

 

Source
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:            
RE:素数环, 字典序输出;
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 int dis[30], vis[30], my[30]; 7 int n; 8 int is_prime(int a) //判断素数; 9 {10 11 if(a == 1 || a == 0)12 return 0;13 if(a == 2)14 return 1;15 else16 {17 for(int i=2; i <= sqrt(a); i++) //不要忘记等号;18 {19 if(a % i == 0)20 return 0; 21 }22 return 1;23 }24 } 25 void Dfs(int a)26 {27 int i;28 if(a == n && is_prime(dis[n-1] + dis[0]))29 {30 for(i = 0; i < n; i++)31 {32 if(i > 0)33 printf(" ");34 printf("%d", dis[i]);35 }36 printf("\n");37 return; //子循环结束; 38 }39 for(i=2; i<=n; i++)40 {41 if(!vis[i] && is_prime(dis[a - 1] + i))42 {43 vis[i] = 1;44 dis[a] = i;45 Dfs(a + 1);46 vis[i] = 0;47 }48 }49 }50 int main()51 {52 int i, j = 1;53 while(~scanf("%d", &n))54 {55 memset(vis, 0 , sizeof(vis));56 printf("Case %d:\n", j++);57 vis[0] = 1;58 dis[0] = 1;59 Dfs(1);60 printf("\n");61 }62 return 0; 63 }

 

 

转载于:https://www.cnblogs.com/soTired/p/4703907.html

你可能感兴趣的文章
记住这两点,彻底终结原型链吧
查看>>
vue .prop修饰符
查看>>
ES6 Promise - 让我们解开的面纱(遵循Promise/A+规范)
查看>>
[菜鸟SpringCloud实战入门]第九章:服务网关Zuul体验
查看>>
MySql中的事务
查看>>
[译] 如何学习 CSS
查看>>
JAVA并发编程之多线程并发同步业务场景与解决方案
查看>>
JS 作用域
查看>>
Android高级开发-布局渲染流程与优化
查看>>
基于JQuery做的一个简单的点击显示和隐藏的小Demo
查看>>
微信小程序开发遇到的bug及填坑
查看>>
基于 Jenkins + JaCoCo 实现功能测试代码覆盖率统计
查看>>
vue写的页面title中ico图标不显示的问题
查看>>
OC_类与对象
查看>>
谈谈CountDownLatch和CyclicBarrier
查看>>
Android之控件与布局,结构知识点,基础完结
查看>>
java B2B2C Springboot多租户电子商城系统-Spring Cloud Stream(消息驱动)
查看>>
CentOS7 0安装jdk + tomcat
查看>>
前端小秘密系列之闭包
查看>>
rocketmq学习杂记
查看>>