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 ...
随机推荐
- 转一篇100offer的采访~35岁程序员是一种什么状态
随着互联网的高速发展变革,大龄恐惧症越来越多地在技术圈被人讨论.很多程序员在工作5-10年以后,都会开始思考5年.10年甚至更久以后的自己,会是怎样一种生活工作状态,以及是否会被时代抛弃. 特别是全民 ...
- Eclipse输入智能提示设置
JAVA智能提示 展开菜单 Window -> preferences -> Java -> Editor -> Content assist 找到右边的Auto-Activa ...
- python import windows文件路经
import sys sys.path.append("E:\\python\\workspacepython\\PY001\\src\\testpy01") import str ...
- E. Reachability from the Capital(tarjan+dfs)
求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...
- 515Nod 1126 求递推序列的第n项【矩阵快速幂】
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
- iOS 集成Protobuf,转换proto文件
原文地址:http://blog.csdn.net/hyq4412/article/details/54891038 附加Homebrew安装地址:https://brew.sh/index_zh-c ...
- 移动端viewport解惑
我们在做移动端webapp的时候需要设置这么一段: <meta name="viewport" content="width=device-width, initi ...
- shell中处理用户输入
1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...
- 首家5G体验厅在深圳建成
日前,深圳移动卓越时代营业厅推出5G全方位体验活动,让市民亲身感受5G时代到来.据悉,十大5G展示项目生动展现移动5G带来的生活巨变与产业升级,为5G发展汇聚各界力量加速创新落地. 现场有市民表示,5 ...
- 洛谷P1164 小A点菜 && caioj 1410 动态规划1:点菜(背包方案问题)
方程很简单 f[0] = 1 f[j] += f[j-w[i]] #include<cstdio> #define REP(i, a, b) for(int i = (a); i < ...