C++ 递归位置排列算法及其应用
废话不多说,我们先看一下位置排序的算法:
#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++ 递归位置排列算法及其应用的更多相关文章
- Java字符串排列算法
Java字符串排列算法 题目:现有ABCDE 5个球 构成的排列组合 可重复抽取 最多取到16个 共有多少种组合方式? 比如:取1个球可以构成的组合有 A B C D E 共5种,取2个球可以构成的组 ...
- Python之find命令中的位置的算法
find("s",a,b) #s表示的是一个子序列,a表示的是检索的起始位置,b表示的是检索的终止位置,ab可有可无 test = "abcdefgh" ...
- JavaScript 递归法排列组合二维数组2
<html> <head> <title>二维数组排列组合</title> </head> <body> <div id= ...
- JavaScript 递归法排列组合二维数组
<html> <head> <title>二维数组排列组合</title> </head> <body> <div id= ...
- justify-content 定义子元素在父元素水平位置排列的顺序
justify-content 定义子元素在父元素水平位置排列的顺序,需要和display:flex使用才会生效. 有五个属性: 1.flex-start(默认值) 左对齐 2.flex-end 右 ...
- 排列算法汇总(下一个排列,全排列,第K个排列)
一.下一个排列 首先,STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. next_permutation(nums.begin() ...
- AcWing 94. 递归实现排列型枚举
AcWing 94. 递归实现排列型枚举 题目链接 把 1~n 这 n 个整数排成一行后随机打乱顺序,输出所有可能的次序. 输入格式 一个整数n. 输出格式 按照从小到大的顺序输出所有方案,每行1个. ...
- 安科 OJ 1054 排队买票 (递归,排列组合)
时间限制:1 s 空间限制:128 M 题目描述 有M个小孩到公园玩,门票是1元.其中N个小孩带的钱为1元,K个小孩带的钱为2元.售票员没有零钱,问这些小孩共有多少种排队方法,使得售票员总能找得开零钱 ...
- (转)排列算法 Permutation Generation
转自:http://www.cnblogs.com/dragonpig/archive/2010/01/21/1653680.html http://www.notesandreviews.com/p ...
随机推荐
- Random words
To choose a random word from the histogram, the simplest algorithm is to build a list with multiple ...
- HikariCP--一款高性能的 JDBC 连接池
源码地址:https://github.com/brettwooldridge/HikariCP 使用方法: Java 8 maven artifact: <dependency> < ...
- Chromium Graphics: Compositor Thread Architecture
Compositor Thread Architecture <jamesr, enne, vangelis, nduca> @chromium.org Goals The main re ...
- 运维派 企业面试题3 为上题中的 "十个随机字母_test.html" 文件 更名
Linux运维必会的实战编程笔试题(19题) 企业面试题3 #将试题2中创建的文件名uopiyhgawe_test.html# test-->修改为omg,html-->HTML 方法一: ...
- d3的一些总结
核心操作:https://blog.csdn.net/kriszhang/article/details/70174410 Update.Enter.Exit 简练详细说明:http://www.cn ...
- Vue2.4.0 新增的inheritAttrs,attrs
官方inheritAttrs,attrs文档https://cn.vuejs.org/v2/guide/components-props.html,从最下面的'非 Prop 的特性'开始看,看到最后 ...
- HTML5按键打开摄像头和拍照
HTML5实现按键打开摄像头和拍照 步骤: 1.创建一个打开摄像头按钮的标签.video标签.拍照的按钮标签.画布 2.实现打开摄像头的功能 3.实现拍照功能 具体实现代码: <!DOCTY ...
- python描述符和属性查找
python描述符 定义 一般说来,描述符是一种访问对象属性时候的绑定行为,如果这个对象属性定义了__get__(),__set__(), and __delete__()一种或者几种,那么就称之为描 ...
- python语法学习笔记
函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来 ...
- HDU 5068 Harry And Math Teacher 线段树+矩阵乘法
题意: 一栋楼有n层,每一层有2个门,每层的两个门和下一层之间的两个门之间各有一条路(共4条). 有两种操作: 0 x y : 输出第x层到第y层的路径数量. 1 x y z : 改变第x层 的 y门 ...