废话不多说,我们先看一下位置排序的算法:

#include <iostream>
using namespace std; int n = 0;
int m = 2;
int l = 0;
int a[100]; void solve(int l); int main()
{ cout<<"请输入位数 n "<<endl;
cin>>n; solve(l);
return 0;
} void solve(int l)
{
if(l>=n)
{
for(int i = 0 ; i<n; i++)
{
cout<<a[i];
}
cout << endl;
return;
}
for(int i = 0 ;i < m;i++)
{
a[l] = i;
solve(l+1); }
}

执行结果例如以下:

请输入位数 n 

4

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

我们能够把这个算法应用到这样一个样例中:

递归求一个集合的全部子集:

代码例如以下:

#include <iostream>
#include<vector>
#include <stdio.h> using namespace std; void solve(int l); int n, m;
int mat[100];
vector<string> collection; int main()
{
string firstElement;
string element; int mcount = 1; cout << "Please input the element , end by #end" << endl;
cin >> firstElement; while(firstElement != "#end")
{
element = firstElement; cout << "The element "<<mcount<< " you input is "+ element<< endl; collection.push_back(element); //cout << collection[mcount-1]; cout << "Please input the next element , end by #end" << endl;
cin >> firstElement;
}
n = collection.size();
m = 2;
solve(0);
return 0;
} void solve(int l)//l=0
{
int i;
if(l >= n)//n=4
{
printf("{");
for(i=0; i<n; ++i)
{
if(mat[i] == 1)
{
cout<< collection[i] << " ";
}
}
printf("}\n");
return;
}
for(i=0; i<m; ++i)//m=2
{
mat[l] = i;
solve(l+1);
}
}

执行结果例如以下:

Please input the element , end by #end

a

The element 1 you input is  a

Please input the next element , end by #end

b

The element 1 you input is  b

Please input the next element , end by #end

c

The element 1 you input is  c

Please input the next element , end by #end

#end

{}

{c }

{b }

{b c }

{a }

{a c }

{a b }

{a b c }

C++ 递归位置排列算法及其应用的更多相关文章

  1. Java字符串排列算法

    Java字符串排列算法 题目:现有ABCDE 5个球 构成的排列组合 可重复抽取 最多取到16个 共有多少种组合方式? 比如:取1个球可以构成的组合有 A B C D E 共5种,取2个球可以构成的组 ...

  2. Python之find命令中的位置的算法

    find("s",a,b)    #s表示的是一个子序列,a表示的是检索的起始位置,b表示的是检索的终止位置,ab可有可无 test = "abcdefgh" ...

  3. JavaScript 递归法排列组合二维数组2

    <html> <head> <title>二维数组排列组合</title> </head> <body> <div id= ...

  4. JavaScript 递归法排列组合二维数组

    <html> <head> <title>二维数组排列组合</title> </head> <body> <div id= ...

  5. justify-content 定义子元素在父元素水平位置排列的顺序

    justify-content 定义子元素在父元素水平位置排列的顺序,需要和display:flex使用才会生效. 有五个属性: 1.flex-start(默认值)  左对齐 2.flex-end 右 ...

  6. 排列算法汇总(下一个排列,全排列,第K个排列)

    一.下一个排列 首先,STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. next_permutation(nums.begin() ...

  7. AcWing 94. 递归实现排列型枚举

    AcWing 94. 递归实现排列型枚举 题目链接 把 1~n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序. 输入格式 一个整数n. 输出格式 按照从小到大的顺序输出所有方案,每行1个. ...

  8. 安科 OJ 1054 排队买票 (递归,排列组合)

    时间限制:1 s 空间限制:128 M 题目描述 有M个小孩到公园玩,门票是1元.其中N个小孩带的钱为1元,K个小孩带的钱为2元.售票员没有零钱,问这些小孩共有多少种排队方法,使得售票员总能找得开零钱 ...

  9. (转)排列算法 Permutation Generation

    转自:http://www.cnblogs.com/dragonpig/archive/2010/01/21/1653680.html http://www.notesandreviews.com/p ...

随机推荐

  1. Android 自定义的开关按钮——SwitchButton

    本文转自:http://blog.csdn.net/swust_chenpeng/article/details/19967501 我将原文的控件进行了一些修改,去掉了原来控件的外边框,只留下重要的遮 ...

  2. grvphviz && dot脚本语言

    安装graphviz 可去官网下载http://www.graphviz.org/download/下载之后按步骤安装 打开编辑器,创建*.dot文件,编辑dot脚本代码,保存. D:\>dot ...

  3. js字符串排序方法

    前端开发过程中有时需自己手写排序方法 一般想到数字的字符串排序方法 我们会用到 var newArr = arr. sort(function(a,b){return a - b})来进行排序 但除此 ...

  4. Python实现快排

    Python实现快排 def quicksort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x ...

  5. 最长回文字串 (The longest palindrome substring)

    这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...

  6. Python 调用snmp自定义OID实现监控

    http://kkkkkk.blog.51cto.com/468162/1172726 http://blog.csdn.net/mirahs/article/details/49619729

  7. View 和 ViewGroup 的 hasFocusable

    在 android 中.焦点的获取和事件几乎相同,有一个分发机制.一般来说View 树上上层节点的 ViewGroup 比底层节点的 View 有更高的优先级获取焦点.这体如今 ViewGroup 有 ...

  8. WEB前端,混合排版,有的宽有的窄,滚动会出现空白处,怎么办。

    多数时候出现空白都是由于有滚动栏滚到一边就会产生空白. overflow-x: hidden; 在最大图的那个div里写这句.

  9. HDU 3911 线段树区间合并

    北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...

  10. IIS Modules Overview

    Introduction The IIS 7 and above Web server feature set is componentized into more than thirty indep ...