HDU 1016 Prime Ring Problem(素数环)
2016-07-22 16:22
369 查看
题目:
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
首先注意到,只有n是偶数的时候才有解,所以实际上n只是2到18这9个偶数而已。
如果追求效率的话,可以硬编码9个字符串,换行的问题还要注意一下。
因为后来提交之后没有出现超时,所以就没有这么做。
既然n最多18,那么2个数的和最多也就35,不超过35的素数只有10个,所以本题的素数判断也是硬编码的。
注意:如果在递归的函数(例如这里的place函数)里面使用嵌套的循环,一定要搞清楚continue和break的作用对象是哪个循环。
代码:
这个题目我出现了几次格式错误,主要是行尾多了一个空格。
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
首先注意到,只有n是偶数的时候才有解,所以实际上n只是2到18这9个偶数而已。
如果追求效率的话,可以硬编码9个字符串,换行的问题还要注意一下。
因为后来提交之后没有出现超时,所以就没有这么做。
既然n最多18,那么2个数的和最多也就35,不超过35的素数只有10个,所以本题的素数判断也是硬编码的。
注意:如果在递归的函数(例如这里的place函数)里面使用嵌套的循环,一定要搞清楚continue和break的作用对象是哪个循环。
代码:
#include<iostream> using namespace std; int list[19];//最多18个数,list[0]不使用 int n; bool ok(int a, int b) { int c = a + b; if (c == 3 || c == 5 || c == 7 || c == 11 || c == 13 || c == 17 || c == 19 || c == 23 || c == 29 || c == 31)return true; return false; } void place(int deep) { if (deep > n) { if (ok(1, list[n])) { for (int i = 1; i <= n; i++) { cout << list[i]; if (i < n)cout << " "; } cout << endl; } return; } for (int i = 2; i <= n; i++) { bool flag = false; for (int j = 1; j < deep; j++) { if (list[j] == i) { flag = true; break; } } if (flag)continue; if (!ok(list[deep - 1], i))continue; list[deep] = i; place(deep + 1); } } int main() { int cas = 1; list[1] = 1; while (cin >> n) { cout << "Case " << cas << ":" << endl; if (n % 2 == 0)place(2); cout << endl; cas++; } return 0; }
这个题目我出现了几次格式错误,主要是行尾多了一个空格。
相关文章推荐
- 线程基础介绍一
- BCB之OLE方式操作Excel
- 医学图像常用特征提取方法
- HDU 3095 哈希+双向搜索
- 使用无符号右移-&-栈 实现 十进制转十六进制
- 看板方法整理
- leetcode_c++:栈:Min Stack(155)
- Floyed算法求多源最短路径
- 10以内的简单计算
- 易流专线货运物流软件 V14.02 网络版
- Android和Untiy3D的交互方式
- 《WINDOWSPE权威指南》学习笔记(一)-U盘监控器的破解
- Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
- PostgreSQL学习----命令或问题小结
- POJ 3252 - Round Numbers(数位dp)
- UITextView 行间距设置以及placeholder
- nginx配置
- serialVersionUID的作用
- 正则表达式
- TAIGA 部署