C + + recursive position permutation algorithm and its application

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.