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

前端 未结 6 1971
挽巷
挽巷 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:04

    I wrote one without a function already implemented even any templates and containers. actually it was written in C first, but has been transform to C++.

    easy to understand but poor efficiency, and its output is what you want, sorted.

    #include 
    #define N 4
    using namespace std;
    
    char ch[] = "abcd";
    
    int func(int n) {
        int i,j;
        char temp;
        if(n==0) {
            for(j=N-1;j>=0;j--)
                cout<i;j--)
                ch[j] = ch[j-1];
            ch[i] = temp;
            //and shift back agian
        }
        return 1;
    }
    
    int main(void)
    {
        func(N);
        return 0;
    }
    

提交回复
热议问题