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 ...
随机推荐
- 使用regasm注册.net com组件出现不是有效的.net程序集的解决办法
作者:朱金灿 来源:http://blog.csdn.net/clever101 在电脑上装有VS 2008和VS 2010.使用VS 2010编写了一个C# com组件:MyCom(基于.net f ...
- python对MySQL进行添加修改删除以及字符串的操作
# coding=UTF-8 import MySQLdb def dbDperate(sql,param): "定义数据库的添加,修改和删除操作" #获取数据库的连接对象 con ...
- 在 yii2.0 框架中封装导出html 表格样式 Excel 类
在 vendor/yiisoft/yii2/helpers/ 创建一个 Excel.php <?php namespace yii\helpers; class Excel{ ...
- Rman备份及不完全恢复操作
最后更新时间:2018/12/18 启用归档 --检查是否为归档 SQL> archive log list; Database log mode No Archive ...
- HDU 4928 Series 2
有了题解以后这题就成了一个模拟题.不过写了好久才把它写对…… Sad #include <iostream> #include <cstdio> #include <cs ...
- PKU 3311 Hie with the Pie 状态DP
Floyd + 状态DP Watashi的板子 #include <cstdio> #include <cstring> #include <iostream> # ...
- POJ——T 1470 Closest Common Ancestors
http://poj.org/problem?id=1470 Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 20830 ...
- POJ 1258 Agri-Net (最小生成树+Prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39820 Accepted: 16192 Descri ...
- Codeforces Round #105 (Div. 2) 148C Terse princess(脑洞)
C. Terse princess time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- HTML——上中下布局
上中下布局是最主要的布局方式,本比如果用户屏幕分辨率为800*600像素. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vuc2h1bWlu/font ...