📜  中央二项式系数

📅  最后修改于: 2021-04-26 09:09:40             🧑  作者: Mango

给定整数N ,任务是找到N^{th}中央二项式系数。
N = 0、1、2、3 ..的前几个中央二项式系数为

例子:

方法:中心二项式系数是以下形式的二项式系数: \binom{2N}{N} 。二项式系数\binom{2N}{N}可以使用这种方法通过动态编程针对给定值N计算该值。

例如:

下面是上述方法的实现:

C++
// C++ implementation to find the 
// Nth Central Binomial Coefficient
  
#include 
using namespace std; 
  
// Function to find the value of 
// Nth Central Binomial Coefficient
int binomialCoeff(int n, int k) 
{ 
    int C[n + 1][k + 1]; 
    int i, j; 
  
    // Calculate value of Binomial
    // Coefficient in bottom up manner 
    for (i = 0; i <= n; i++) 
    { 
        for (j = 0; j <= min(i, k); j++) 
        { 
            // Base Cases 
            if (j == 0 || j == i) 
                C[i][j] = 1; 
  
            // Calculate value 
            // using previously 
            // stored values 
            else
                C[i][j] = C[i - 1][j - 1] + 
                        C[i - 1][j]; 
        } 
    } 
  
    return C[n][k]; 
} 
  
// Driver Code 
int main() 
{ 
    int n = 3;
    int k = n;
    n = 2*n;
    cout << binomialCoeff(n, k); 
}


Java
// Java implementation to find the 
// Nth Central Binomial Coefficient
class GFG{
      
// Function to find the value of 
// Nth Central Binomial Coefficient
static int binomialCoeff(int n, int k) 
{ 
    int[][] C = new int[n + 1][k + 1]; 
    int i, j; 
  
    // Calculate value of Binomial
    // Coefficient in bottom up manner 
    for(i = 0; i <= n; i++) 
    { 
       for(j = 0; j <= Math.min(i, k); j++) 
       { 
             
          // Base Cases 
          if (j == 0 || j == i) 
              C[i][j] = 1; 
            
          // Calculate value 
          // using previously 
          // stored values 
          else
              C[i][j] = C[i - 1][j - 1] + 
                        C[i - 1][j]; 
       } 
    } 
    return C[n][k]; 
} 
  
// Driver Code 
public static void main(String[] args)
{ 
    int n = 3;
    int k = n;
    n = 2 * n;
      
    System.out.println(binomialCoeff(n, k)); 
}
}
  
// This code is contributed by Ritik Bansal


Python3
# C# implementation to find the 
# Nth Central Binomial Coefficient
  
# Function to find the value of 
# Nth Central Binomial Coefficient
def binomialCoeff(n, k):
      
    C = [[0 for j in range(k + 1)] 
            for i in range(n + 1)]
      
    i = 0
    j = 0
      
    # Calculate value of Binomial
    # Coefficient in bottom up manner
    for i in range(n + 1):
        for j in range(min(i, k) + 1):
              
            # Base Cases
            if j == 0 or j == i:
                C[i][j] = 1
                  
            # Calculate value 
            # using previously 
            # stored values 
            else:
                C[i][j] = (C[i - 1][j - 1] +
                           C[i - 1][j])
      
    return C[n][k]
      
# Driver code
if __name__=='__main__':
      
    n = 3
    k = n
    n = 2 * n
      
    print(binomialCoeff(n, k))
          
# This code is contributed by rutvik_56


C#
// C# implementation to find the 
// Nth Central Binomial Coefficient
using System;
class GFG{
      
// Function to find the value of 
// Nth Central Binomial Coefficient
static int binomialCoeff(int n, int k) 
{ 
    int [,]C = new int[n + 1, k + 1]; 
    int i, j; 
  
    // Calculate value of Binomial
    // Coefficient in bottom up manner 
    for(i = 0; i <= n; i++) 
    { 
       for(j = 0; j <= Math.Min(i, k); j++)
       {
             
          // Base Cases 
          if (j == 0 || j == i) 
              C[i, j] = 1; 
                
          // Calculate value 
          // using previously 
          // stored values 
          else
              C[i, j] = C[i - 1, j - 1] + 
                        C[i - 1, j]; 
       } 
    } 
    return C[n, k]; 
} 
  
// Driver Code 
public static void Main()
{ 
    int n = 3;
    int k = n;
    n = 2 * n;
      
    Console.Write(binomialCoeff(n, k)); 
}
}
  
// This code is contributed by Code_Mech


输出:
20