Details based on how many permutation and combination outputs

  • 2020-04-01 23:27:00
  • OfStack

arrangement


#include <stdio.h>
//The main thing is to find the relationship between the current and the number of rows
int swap(int m,int n)
{
 if(n==1)
  return m-n+1;
 return  m*swap(m-1,n-1);
}
int main()
{
 int m=5,n=4;
 printf("%d",swap(5,4));
}

combination

Calculate how many permutations three A's and two B's make up

Idea 1:


#include <stdio.h>

int f(int m, int n)
{
 if(m==0 || n==0) return 1;
    return f(m,n-1)+f(m-1,n);
}
void main ()
{ 
  printf("%d ",f(3,2)); 
}

Idea 2:

#include <stdio.h>
#include <math.h>
/*
        for (m+n)! The permutation method is calculated for all elements without repeating, 
     If there are duplicates, you need to filter out the permutations of those duplicates. 
     So we can use the binding method, we can bind the same elements together, because it's a combination, so the arrangement of the internal elements is not considered, 
     There are all of these permutations m ! and n ! , so we can get ( m+n)!/(m!*n!) The ranking method.  
    m a A n a B The permutations of (m+n)!/(m!*n!)
  while m-1 a A n-1 a B The permutations of (m+n-2)!/((m-1)!*(n-1)!)
  so m a A n a B The arrangement of the number =m-1 a A n-1 a B The arrangement of the number *(m+n)*(m+n-1)/(m*n)  
    The key is to find ( m The arrangement of A's and n B's) and (m minus 1 A's and n minus 1 B's) 
*/
int f(int m, int n)
{
 if(m==0 || n==0) return 1;
    return f(m-1,n-1)*(m+n-1)*(m+n)/m/n;
}
void main ()
{ 
  printf("%d ",f(3,2)); 
}


Related articles: