-
This is the classic all-permutation algorithm. m and k represent the range of elements to be fully arranged, i.e., the index of the two endpoints, m is the beginning index, and k is the end endpoint index.
template
k and m represent the range of elements that need to be fully arranged The index at both endpoints
void perm(type list,int k, int m)
if(k==m) is narrowed down to 0
for(int i=0; i<=m; i++)
cout swap(list[k],list[j]);Elements subscripted k and j swap their positions in the array.
perm(list, k+1, m);*k+1 per call, i.e. the full range is reduced by 1*
swap(list[k],list[j]);When backtracking, revert to the previous state.
Let me tell you about the idea of algorithms:
Suppose we find the full permutation of Permute (ABC). Full permutation of Permute(ABC) = A+Permute(BC) and B+Permute(AC) and C+Permute(Ab)=.........And so on. So you can do it with recursion.
And the process of splitting ABC into A+BC, B+AC, C+AB is the above:
for(int j=k; j<=m; j++)
swap(list[k],list[j]);
perm(list, k+1, m);
swap(list[k],list[j]);
The process. After it is done, it has to be changed back, restored to the original state, and then done.
When there is only 1 letter at the end, i.e. k=m, there is only one case, and thus the output.
That's the general idea. It's basically all talked.
I don't understand, hi me.
If the landlord can do it, let's say so. Pure hand-played.
-
void perm(type list,int k, int m)if(k==m) is reduced to 0for(int i=0; i<=m; i++)cout perm(list, k+1, m);*k+1 per call, i.e. the full range is reduced by 1*
swap(list[k],list[j]);When backtracking, revert to the previous state.
-
Find a full permutation algorithm. The landlord will leave after asking, what is his attitude?
-
For example: Changmo C (2 above, 3 below) = (3*2) (2*1) = 3. The above number stipulates that the number of several reed cracks is multiplied, and the number is from large to small.
Taking out m different elements (0 m n) from n different elements at a time, regardless of their order, is called selecting a combination of m elements from n elements without repeating.
-
The focus of recursive function understanding is in the else branch, where the comm(n,k) function is represented from 1,2 ,..Take any combination of k numbers in n numbers. We can take the numbers one by one, and there are 2 possible options for each number:
1) When this number is chosen, then the following number is a combination of k-1 numbers from the remaining n-1 numbers. This is indicated by the following.
comm(n - 1, k - 1)
2) When this number is not selected, then the following number is the combination of k numbers from the remaining n-1 numbers. This is indicated by the following.
comm(n - 1, k)
Therefore, the general possibility is to add these two possibilities together, for:
comm(n - 1, k) +comm(n - 1, k - 1)
-
for(j=0;ja[j-1])
The first loop, j=0, if in a[j-1], what, a[-1]?
This is called the all-wrong permutation problem, and it was first answered by Euler. We might as well take n people as f(n), then f(n)=(n-1)[f(n-1)+f(n-2)].f(0)=0,f(1)=1. >>>More
Let n=2k+1, then p(m=n) = c(2k,k) *1 2) (2k+1) *1 (k+1), where c(n,m) represents the number of different combinations of m in n numbers. >>>More
The formula for calculating the combination of the chop and the row:Permutation a(n,m) = n (n-1). (n-m+1)=n!/(n-m)!(n is the subscript, m is the superscript, the same below). >>>More
I don't know if the balls are the same.
You can get some stimulants. >>>More
LZ: This is a typical mistake of yours, this calculation must be repeated, and to understand it this way: now there are athletes with numbers of , (the first 6 are male athletes, and the last 4 are female athletes) Think like you think about it, if you choose a female athlete with a number 7 for the first time, and choose four from the remaining 9 for the second time, if the selected four include an athlete with a number 8, this situation is the same as the first time the female athlete is numbered 8, and the second time you choose a number 7, so it is repeated So it can't be counted like that, it can only be classified like the answer: >>>More