给定正整数n 。任务是在所有二项式系数中找到最大系数项。
二项式系数序列为
n C 0 , n C 1 , n C 2 ,…。, n C r ,…。, n C n-2 , n C n-1 , n 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 :(使用公式)
证明,
Expansion of (x + y)n are:
nC0 xn y0, nC1 xn-1 y1, nC2 xn-2 y2, …., nCr xn-r yr, …., nCn-2 x2 yn-2, nCn-1 x1 yn-1, nCn x0 yn
So, putting x = 1 and y = 1, we get binomial coefficient,
nC0, nC1, nC2, …., nCr, …., nCn-2, nCn-1, nCn
Let term ti+1 contains the greatest value in (x + y)n. Therefore,
tr+1 >= tr
nCr xn-r yr >= nCr-1 xn-r+1 yr-1
Putting x = 1 and y = 1,
nCr >= nCr-1
nCr/nCr-1 >= 1
(using nCr/nCr-1 = (n-r+1)/r)
(n-r+1)/r >= 1
(n+1)/r – 1 >= 1
(n+1)/r >= 2
(n+1)/2 >= r
Therefore, r should be less than equal to (n+1)/2.
And r should be integer. So, we get maximum coefficient for r equals to:
(1) n/2, when n is even.
(2) (n+1)/2 or (n-1)/2, when n is odd.
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