📜  最大二项式系数项值

📅  最后修改于: 2021-05-06 17:20:38             🧑  作者: Mango

给定正整数n 。任务是在所有二项式系数中找到最大系数项。
二项式系数序列为
n C 0n C 1n C 2 ,…。, n C r ,…。, n C n-2n C n-1n C n
任务是找到n C r的最大值
例子:

Input : n = 4
Output : 6
4C0 = 1
4C1 = 4
4C2 = 6
4C3 = 1
4C4 = 1
So, maximum coefficient value is 6.

Input : n = 3
Output : 3

方法1 :(强力)
想法是找到二项式系数系列的所有值并找到该系列的最大值。
以下是此方法的实现:

C++
// CPP Program to find maximum binomial coefficient
// term
#include
using namespace std;
 
// Return maximum binomial coefficient term value.
int maxcoefficientvalue(int n)
{
    int C[n+1][n+1];
  
    // Calculate value of Binomial Coefficient in
    // bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= min(i, n); 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];
        }
    }
         
    // finding the maximum value.
    int maxvalue = 0;
    for (int i = 0; i <= n; i++)
        maxvalue = max(maxvalue, C[n][i]);
         
    return maxvalue;
}
 
// Driven Program
int main()
{
    int n = 4;
    cout << maxcoefficientvalue(n) << endl;
    return 0;
}


Java
// Java Program to find
// maximum binomial
// coefficient term
import java.io.*;
 
class GFG
{
// Return maximum binomial
// coefficient term value.
static int maxcoefficientvalue(int n)
{
    int [][]C = new int[n + 1][n + 1];
 
    // Calculate value of
    // Binomial Coefficient 
    // in bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0;
                 j <= Math.min(i, n); 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];
        }
    }
         
    // finding the
    // maximum value.
    int maxvalue = 0;
     
    for (int i = 0; i <= n; i++)
        maxvalue = Math.max(maxvalue, C[n][i]);
         
    return maxvalue;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 4;
    System.out.println(maxcoefficientvalue(n));
}
}
 
// This code is contributed by ajit


Python3
# Python3 Program to find
# maximum binomial
# coefficient term
 
# Return maximum binomial
# coefficient term value.
def maxcoefficientvalue(n):
    C = [[0 for x in range(n + 1)]
            for y 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, n) + 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]);
     
    # finding the maximum value.
    maxvalue = 0;
    for i in range(n + 1):
        maxvalue = max(maxvalue, C[n][i]);
     
    return maxvalue;
 
# Driver Code
n = 4;
print(maxcoefficientvalue(n));
 
# This code is contributed by mits


C#
// C# Program to find maximum binomial coefficient
// term
using System;
 
public class GFG {
         
    // Return maximum binomial coefficient term value.
    static int maxcoefficientvalue(int n)
    {
        int [,]C = new int[n+1,n+1];
     
        // Calculate value of Binomial Coefficient in
        // bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= Math.Min(i, n); 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];
            }
        }
             
        // finding the maximum value.
        int maxvalue = 0;
         
        for (int i = 0; i <= n; i++)
            maxvalue = Math.Max(maxvalue, C[n,i]);
             
        return maxvalue;
    }
     
    // Driven Program
 
    static public void Main ()
    {
         
        int n = 4;
         
        Console.WriteLine(maxcoefficientvalue(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP Program to find maximum binomial coefficient term
#include
using namespace std;
 
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
    int C[n+1][k+1];
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int 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];
}
 
// Return maximum binomial coefficient term value.
int maxcoefficientvalue(int n)
{
    // if n is even
    if (n%2 == 0)
        return binomialCoeff(n, n/2);
         
    // if n is odd
    else
        return binomialCoeff(n, (n+1)/2);
}
 
// Driven Program
int main()
{
    int n = 4;
    cout << maxcoefficientvalue(n) << endl;
    return 0;
}


Java
// Java Program to find
// maximum binomial
// coefficient term
import java.io.*;
 
class GFG
{
             
    // Returns value of
    // Binomial Coefficient
    // C(n, k)
    static int binomialCoeff(int n,
                             int k)
    {
        int [][]C = new int[n + 1][k + 1];
     
        // Calculate value of
        // Binomial Coefficient
        // in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int 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];
    }
     
    // Return maximum
    // binomial coefficient
    // term value.
    static int maxcoefficientvalue(int n)
    {
         
        // if n is even
        if (n % 2 == 0)
            return binomialCoeff(n, n / 2);
             
        // if n is odd
        else
            return binomialCoeff(n, (n + 1) / 2);
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
     
        System.out.println(maxcoefficientvalue(n));
    }
}
 
// This code is contributed
// by akt_mit


Python3
# Python3 Program to find
# maximum binomial
# coefficient term
# Returns value of
# Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
 
    C=[[0 for x in range(k+1)] for y 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];
 
 
# Return maximum binomial
# coefficient term value.
def maxcoefficientvalue(n):
    # if n is even
    if (n % 2 == 0):
        return binomialCoeff(n, int(n / 2));
         
    # if n is odd
    else:
        return binomialCoeff(n, int((n + 1) / 2));
 
# Driver Code
if __name__=='__main__':
    n = 4;
    print(maxcoefficientvalue(n));
 
# This code is contributed by mits


C#
// C# Program to find maximum binomial
// coefficient term
using System;
 
public class GFG {
         
    // Returns value of Binomial Coefficient
    // C(n, k)
    static int binomialCoeff(int n, int k)
    {
        int [,]C = new int[n+1,k+1];
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int 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];
    }
     
    // Return maximum binomial coefficient
    // term value.
    static int maxcoefficientvalue(int n)
    {
         
        // if n is even
        if (n % 2 == 0)
            return binomialCoeff(n, n/2);
             
        // if n is odd
        else
            return binomialCoeff(n, (n + 1) / 2);
    }
     
    // Driven Program
    static public void Main ()
    {
         
        int n = 4;
         
        Console.WriteLine(maxcoefficientvalue(n));
    }
}
 
// This code is contributed by vt_m.


PHP


输出:

6

方法2 :(使用公式)

证明,

C++

// CPP Program to find maximum binomial coefficient term
#include
using namespace std;
 
// Returns value of Binomial Coefficient C(n, k)
int binomialCoeff(int n, int k)
{
    int C[n+1][k+1];
  
    // Calculate value of Binomial Coefficient
    // in bottom up manner
    for (int i = 0; i <= n; i++)
    {
        for (int 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];
}
 
// Return maximum binomial coefficient term value.
int maxcoefficientvalue(int n)
{
    // if n is even
    if (n%2 == 0)
        return binomialCoeff(n, n/2);
         
    // if n is odd
    else
        return binomialCoeff(n, (n+1)/2);
}
 
// Driven Program
int main()
{
    int n = 4;
    cout << maxcoefficientvalue(n) << endl;
    return 0;
}

Java

// Java Program to find
// maximum binomial
// coefficient term
import java.io.*;
 
class GFG
{
             
    // Returns value of
    // Binomial Coefficient
    // C(n, k)
    static int binomialCoeff(int n,
                             int k)
    {
        int [][]C = new int[n + 1][k + 1];
     
        // Calculate value of
        // Binomial Coefficient
        // in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int 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];
    }
     
    // Return maximum
    // binomial coefficient
    // term value.
    static int maxcoefficientvalue(int n)
    {
         
        // if n is even
        if (n % 2 == 0)
            return binomialCoeff(n, n / 2);
             
        // if n is odd
        else
            return binomialCoeff(n, (n + 1) / 2);
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
     
        System.out.println(maxcoefficientvalue(n));
    }
}
 
// This code is contributed
// by akt_mit

Python3

# Python3 Program to find
# maximum binomial
# coefficient term
# Returns value of
# Binomial Coefficient C(n, k)
def binomialCoeff(n, k):
 
    C=[[0 for x in range(k+1)] for y 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];
 
 
# Return maximum binomial
# coefficient term value.
def maxcoefficientvalue(n):
    # if n is even
    if (n % 2 == 0):
        return binomialCoeff(n, int(n / 2));
         
    # if n is odd
    else:
        return binomialCoeff(n, int((n + 1) / 2));
 
# Driver Code
if __name__=='__main__':
    n = 4;
    print(maxcoefficientvalue(n));
 
# This code is contributed by mits

C#

// C# Program to find maximum binomial
// coefficient term
using System;
 
public class GFG {
         
    // Returns value of Binomial Coefficient
    // C(n, k)
    static int binomialCoeff(int n, int k)
    {
        int [,]C = new int[n+1,k+1];
     
        // Calculate value of Binomial
        // Coefficient in bottom up manner
        for (int i = 0; i <= n; i++)
        {
            for (int 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];
    }
     
    // Return maximum binomial coefficient
    // term value.
    static int maxcoefficientvalue(int n)
    {
         
        // if n is even
        if (n % 2 == 0)
            return binomialCoeff(n, n/2);
             
        // if n is odd
        else
            return binomialCoeff(n, (n + 1) / 2);
    }
     
    // Driven Program
    static public void Main ()
    {
         
        int n = 4;
         
        Console.WriteLine(maxcoefficientvalue(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


输出:

6