Needless to say, let's look at the algorithm for positional sorting:
#include <iostream>using namespace Std;int n = 0;int m = 2;int L = 0;int a[100];void solve (int l); int main () {cout< ;< "Please enter the number of digits 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);}}
The results of the operation are as follows:
Please enter the number of digits n
4
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
We can apply this algorithm to such an example:
To recursively find all subsets of a collection:
The code is as follows:
#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 are 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); }}
The results of the operation are as follows:
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 + + recursive position permutation algorithm and its application