(step 4.3.3)hdu 1016(Prime Ring Problem——DFS)
来源:互联网 发布:封锁 张爱玲 知乎 编辑:程序博客网 时间:2024/05/29 08:38
题目大意:输入一个整数n,输出1~n个数所能组成的所有互质数列(两两互质)
解题思路:DFS
代码如下:
/* * 1016_2.cpp * * Created on: 2013年8月17日 * Author: Administrator * 章泽天,我的女神。。。。 */#include <iostream>using namespace std;int hash[21]; //hash[] :用来标记1~30之间的数字的使用情况int arr[21]; //arr[i] :第i个位置上的数字是。。。。int n; //数字的数量/** * 判断数字是否是一个素数(质数) * 质数:除了1和它本身不能被其他任何数整除 */bool check(int x) {int i;for (i = 2; i < x; ++i) {if (x % i == 0) {return false;}}return true;}void dfs(int order, int index) {//arr[order] = index :序列中,第order个位置上的数是indexarr[order] = index;hash[index] = true;int i;//如果当前搜索的深度==指定搜索的深度if (order == n) {//判断最后一个数和第一个数是否互质if (check(arr[order] + arr[1])) {/** * 输出满足条件的序列 */printf("1");for (i = 2; i <= n; ++i) {printf(" %d", arr[i]);}printf("\n");}}for (i = 1; i <= n; ++i) {//如果当前的数字i没有使用过&&它与前面一个数字互质if (!hash[i] && check(arr[order] + i)) {dfs(order + 1, i); //将i放进序列,进行下一层的搜索hash[i] = 0; //注意回溯}}}int main() {int count = 1; //用来记数while (scanf("%d", &n) != EOF) {printf("Case %d:\n", count++);memset(hash, 0, sizeof(hash));dfs(1, 1);printf("\n");}}
- (step 4.3.3)hdu 1016(Prime Ring Problem——DFS)
- hdu 1016——Prime Ring Problem(DFS)
- 【HDU 1016】Prime Ring Problem —— DFS
- 1016Prime Ring Problem——dfs
- Prime Ring Problem hdu 1016 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】
- HDU 1016Prime Ring Problem简单dfs
- hdu 1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem (DFS)
- HDU 1016 Prime Ring Problem 简单DFS
- HDU 1016Prime Ring Problem(dfs)
- hdu 1016 Prime Ring Problem (dfs)
- hdu 1016 Prime Ring Problem dfs
- Erlang程序的性能测试工具(1)
- spoj FTOUR2(树的分治)
- linux 临时补充
- Qt样式表
- a different object with the same identifier value was already associated with the session:
- (step 4.3.3)hdu 1016(Prime Ring Problem——DFS)
- 给大家带来HTML 4.01 快速参考
- 几款实用免费的web打印控件
- linux 网络编程 IO复用解释
- Struts2的action中处理JSONP方式提交的中文乱码问题:
- AS3.0旋转过渡效果
- codeforces 337C Quiz
- 对 Linux 专家非常有用的 20 个命令
- Kdevelop的安装和使用