C language implements a full permutation algorithm template method
- 2020-06-23 01:31:52
- OfStack
The main idea of the program is:
1. Change the first number to the front (it's already there), print 1xx, and arrange the last two Numbers 2 and 3 in full order.
2. Change the second number to the front to print 2xx and arrange the last two Numbers 1 and 3 in full order.
3. Change the third number to the front to print 3xx and arrange the last two Numbers 1 and 2 in full order.
So this is a recursive process, the problem of doing a full permutation of the whole sequence comes down to the problem of doing a full permutation of its subsequences, and notice I didn't describe how Base Case does it, you have to think about it. If you change the definition of N and the array a (for example, to a four-digit array), the rest of the code will be able to do all four permutations (a total of 24 permutations) without modification.
Solution process:
1. When N = 1, the sequence can be printed directly.
2. When N = 2, set the array to [a, b]
Print a[0], a[1] (i.e. a, b)
Exchange a[0],a[1] contents
Print a[0],a[1] (this becomes [b, a])
3. When N = 3, the array is [a, b, c]
3.1 Place a in the position of a[0] (as originally, a[0] = a[0]) and print the full array of b,c (i.e., the full array of a[1], a[2])
3.2 Put b at the position of a[0] (at this time, it is necessary to exchange the original array of a[0] and a[1]), then print the full arrangement of a and c, and then change back to the original position, that is, a is still restored to a[0], and b is also restored to the position of a[1]
3.3 Put c at the position of a[0] (at this time, a[0] and a[2] of the original array need to be exchanged), then print the full arrangement of a and b, and then change back to the original position after printing, that is, a will still return to a[0], and b will return to the position of a[1]
At this point, the permutation is complete
When N = 4,5,6... "And so on.
#include <stdio.h>
/************************************************************************/
/* Function: Realize the exchange of two shaping parameter values
/* Parameters:
/* lhs--int Pointer of type to the number to be exchanged 1 The address of the
/* rhs--int Pointer of type to the number to be exchanged 2 The address of the
/************************************************************************/
void Swap(int *lhs, int *rhs)
{
int t = *lhs;
*lhs = *rhs;
*rhs = t;
}
/************************************************************************/
/* Function: Realize full arrangement function
/* Parameters:
/* source-- An integer array that holds all elements that need to be sorted
/* begin -- To find the 1 The starting position of the array
/* end -- To find the 1 The end of a permutation, when begin=end , indicates completion 1 A permutation
/************************************************************************/
void FullPermutation(int source[], int begin, int end)
{
int i;
if (begin >= end) // find 1 A permutation
{
for (i = 0; i < end; i++)
{
printf("%d", source[i]);
}
printf("\n");
}
else// Did not find out 1 If you have a permutation, you keep going down 1 An element
{
for (i = begin; i < end; i++)
{
if (begin != i)
{
Swap(&source[begin], &source[i]); // exchange
}
// Recursively arrange the remaining slaves begin+1 to end The elements of the
FullPermutation(source, begin + 1, end);
if (begin != i)
{
Swap(&source[begin], &source[i]); // Retroactive reduction
}
}
}
}
int main()
{
int source[30];
int i, count;
scanf("%d", &count);
// Initializing array
for (i = 0; i < count; i++)
{
source[i] = i + 1;
}
FullPermutation(source, 0, count);
return 0;
}