Home > Back-end >  C language to achieve full arrangement
C language to achieve full arrangement

Time:03-30

full permutation problem:
Take from n different elements m (m, n) or less elements, according to certain order, called from the n different elements m an arrangement of elements, when m=n all arrangement of permutations,
Such as: 1, 2, 3 full arrangement of three elements: 1, 2, 3; 1 31; 2,1,3; 2,3,1; 3,1,2; 3, 2, 1

Specific process:
1. The first one, to exchange for all 23:123132;
2. The exchange of 2, 1, permutations of 13:213231;
3. The exchange of 3, 1, for all 21:321312;


/* * * * * * * * * * * * * * * all arrangement problems * * * * * * * * * * * * * * * * */
# include
# define N 10000

//implementation two plastic parameter value exchange
Void Swap (int * x, int * y)
{
int temp;
Temp=* x;
* x=* y;
* y=temp;
}

//implementation permutations function
Int Fullpermutation (int arr [], int the begin, int the end)
{//be starting position, en end
int i;
If (the begin & gt;=end)//to find a permutation
{
For (I=0; i {
Printf (" % d ", arr [I]);
}
printf("\n");
}
Finish else//not to find a permutation, continued to find the next element
{
For (I=begin; i {
If (the begin!=I)
{
Swap (& amp; Arr (begin), & amp; Arr [I]);//exchange
}

//from the rest of recursive arrangement be + 1 to en element
Fullpermutation (arr, begin + 1, end);

If (the begin!=I)
{
Swap (& amp; Arr (begin), & amp; Arr [I]);//back to restore
}
}
}
}

Int main ()
{
Int n, I;
An int array [N].
The scanf (" % d ", & amp; N);
//initialize array
for(i=0; i{
Array [I]=I + 1;
}

Fullpermutation (array, 0, n);
return 0;
}

run results:

CodePudding user response:

  • Related