How to generate all permutations of an array in sorted order?

前端 未结 6 1978
挽巷
挽巷 2020-11-29 04:48

I have an array, and the user can insert a string.

And I have this code:

int main(){
  char anagrama[13];
  cin >> anagrama;
  for(int j = 0; j         


        
6条回答
  •  感动是毒
    2020-11-29 05:13

    @alexander the output of this programme is in exact order as requested by you:

    HERE, is a simplest code for generating all combination/permutations of a given array without including some special libraries (only iostream.h and string are included) and without using some special namespaces than usual ( only namespace std is used).

    void shuffle_string_algo( string ark )
    {
    
    //generating multi-dimentional array:
    
    char** alpha = new char*[ark.length()];
    for (int i = 0; i < ark.length(); i++)
        alpha[i] = new char[ark.length()];
    
    //populating given string combinations over multi-dimentional array
    for (int i = 0; i < ark.length(); i++)
        for (int j = 0; j < ark.length(); j++)
            for (int n = 0; n < ark.length(); n++)
                if( (j+n) <= 2 * (ark.length() -1) )
                    if( i == j-n)
                        alpha[i][j] = ark[n];
                    else if( (i-n)== j)
                        alpha[i][j] = ark[ ark.length() - n];
    
    if(ark.length()>=2)
    {
        for(int i=0; i=2)
            {
                //review the logic to get the working idea of v++ and v--
                v++;
                shuffle_string_algo( send_this);
                v--;
            }
            else
            {
                //if, further combinations are not possiable print these combinations 
                ma[v] = alpha[i][0];
                ma[++v] = alpha[i][1];
                ma[++v] = '\0';
                v=v-2;
    
                string disply(ma);
                cout<<++permutaioning<<":\t"<

    and main:

    int main()
    {
    string a;
    int ch;
    do
    {
        system("CLS");
        cout<<"PERMUNATING BY ARK's ALGORITH"<>ch;
    } while (ch!=0);
    
    return 0;
    }
    

    HOPE! it helps you! if you are having problem with understanding logic just comment below and i will edit.

提交回复
热议问题