HDU 1016 (Prime Ring Problem)
来源:互联网 发布:mac诸星团最后去了哪里 编辑:程序博客网 时间:2024/06/05 03:20
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 53427 Accepted Submission(s): 23627
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
#include <stdio.h>int a[100],b[100]; //全局变量的数组默认初始化为0 int n;int prime(int k){ int f=0; if(k==1) return 1;for(int i=2; i<=k-1; i++){if(k%i==0) f=1;}return f;}void dfs(int x){if(x==n) //当搜索到最后一个数时 {if(prime(a[x]+1)==0) //判断这个数与1的和是否为素数 {int i; printf("1");for( i=2; i<=n; i++){printf(" %d",a[i]); //如果是的话则从1开始输出排好序的数组 } printf("\n"); }return;}for(int j=2; j<=n; j++) {if(b[j]==0 && prime(a[x]+j)==0) //如果找到一个未被标记的数并满足其与前一个数之和为素数 {b[j]=1; //标记这个数并从这个数开始进行下一轮搜索 a[x+1]=j; dfs(x+1);b[j]=0; //回溯时重新标记为0 }}return;}int main(){int y=1;while(scanf("%d",&n)!=EOF){printf("Case %d:\n",y++);a[1]=1;b[1]=1; //b数组用来对1之后的每个数进行标记,如果搜索一次后标记为1,回溯时再标记为0 dfs(1);printf("\n");}return 0;}
阅读全文
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 1016Prime 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
- 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
- linux下的一些常用命令整理
- 廖雪峰Python教程阅读笔记——2. Python函数
- xcode 代码前不显示行数,解决方法
- HDU 6130 Kolakoski(数学)
- 8-15 DAIRY
- HDU 1016 (Prime Ring Problem)
- Python numpy函数hstack() vstack() stack() dstack() vsplit() concatenate()
- Hdu6127 Hard challenge(2017多校第7场)
- 递归与动态规划---跳跃游戏
- 通过JDBC访问数据库
- 刷题——Stripies POJ
- spring boot集成activemq
- Frequent values(POJ-1806)(RMQ)
- 一次小测试(+摸鱼)