📜  给定水平的Pascal三角形中所有整数的最大值

📅  最后修改于: 2021-04-22 03:28:16             🧑  作者: Mango

给定一个整数L ,任务是找到在给定水平上在Pascal三角形中存在的所有整数中的最大值。
一个具有6个级别的Pascal三角形如下所示:

例子:

方法:众所周知,在一个Pascal三角每一行是二项式系数和k系数在用于电平nN ck中的二项展开式。同样,任何级别的中间元素始终最大,即k = floor(n / 2)
因此,在Pascal三角形的给定水平上存在的所有整数的最大值为binomialCoeff(n,n / 2)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function for the 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];
}
  
// Function to return the maximum
// value in the nth level
// of the Pascal's triangle
int findMax(int n)
{
    return binomialCoeff(n, n / 2);
}
  
// Driver code
int main()
{
    int n = 5;
  
    cout << findMax(n);
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG
{
    // Function for the 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];
    }
      
    // Function to return the maximum
    // value in the nth level
    // of the Pascal's triangle
    static int findMax(int n)
    {
        return binomialCoeff(n, n / 2);
    }
      
    // Driver code
    public static void main (String[] args) {
          
        int n = 5;
      
        System.out.println(findMax(n));
      
    }
  
}
  
  
// This code is contributed by ihritik


C#
// C# implementation of the approach
  
using System;
class GFG
{
    // Function for the 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];
    }
      
    // Function to return the maximum
    // value in the nth level
    // of the Pascal's triangle
    static int findMax(int n)
    {
        return binomialCoeff(n, n / 2);
    }
      
    // Driver code
    public static void Main () {
          
        int n = 5;
      
        Console.WriteLine(findMax(n));
      
    }
  
}
  
  
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
  
# Function for the binomial coefficient
def binomialCoeff(n, k):
    C = [[0 for i in range(k + 1)] 
            for i in range(n + 1)]
  
    # 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]
  
# Function to return the maximum
# value in the nth level
# of the Pascal's triangle
def findMax(n):
    return binomialCoeff(n, n // 2)
  
# Driver code
n = 5
  
print(findMax(n))
  
# This code is contributed by Mohit Kumar


输出:
10