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

#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. 84.Node.js -Mongoose 方法

    转自:https://www.cnblogs.com/chris-oil/p/9136534.html Mongoose 参考手册 标签(空格分隔): MongoDB Mongoose 是什么? 一般 ...

  2. Struts2标签库整理【完整】

    转自:https://blog.csdn.net/chen_zw/article/details/8161230   Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,str ...

  3. SharePoint InfoPath 保存无法发布问题

    设计完表单以后提示以下错误 错误描述 InfoPath无法保存下列表单:******* 此文档库已被重命名或删除,或者网络问题导致文件无法保存.如果此问题持续存在,请于网络管理员联系. 可参考网站 & ...

  4. cuda thrust函数首次调用耗费时间比后续调用长原因

    lazy context initialisation. stackoverflow

  5. vue的鼠标移入和移出

    vue的鼠标移入和移出 需求(鼠标到预约二维码显示,预约添加背景色) 实现 <!--html部分--> <ul class="person_list"> / ...

  6. Redis特点以及安装

       Mysql 的数据 是以"文件形式存储在硬盘"里边.硬盘运行速度相比较CPU.内存是排在第三的.而 Redis 是内存高速缓存数据库,运行速度比 Mysql 速度快,也支持数 ...

  7. appium使用教程(一 环境搭建)-------------1.准备阶段

    1.准备: 下载需要的安装文件等. 1) Appium(由于某种原因,官网下载会下载一半就挂掉: http://appium.io/).建议去网盘下载 2) . net framework 4.0  ...

  8. Decorator - 利用装饰器武装前端代码

    历史 以前做后端时,接触过一点Spring,也是第一次了解DI.IOC等概念,面向切面编程,对于面向对象编程还不怎么熟练的情况下,整个人慌的一批,它的日志记录.数据库配置等都非常方便,不回侵入到业务代 ...

  9. FOJ1205 小鼠迷宫问题 (BFD+递推)

    FOJ1205 小鼠迷宫问题 (BFD+递推) 小鼠a与小鼠b身处一个m×n的迷宫中,如图所示.每一个方格表示迷宫中的一个房间.这m×n个房间中有一些房间是封闭的,不允许任何人进入.在迷宫中任何位置均 ...

  10. Liquibase被锁

    经常运行过程中出现 Liquibase - Waiting for changelog lock Waiting for changelog lock.... Running the migratio ...