给定最大整数,打印二项式系数表将打印所有二项式系数B(m,x),其中m和x从0到最大值变化
例子 :
Input : max = 3
Output :
0 1
1 1 1
2 1 2 1
3 1 3 3 1
解释什么是二项式系数最简单的方法是说,它们计算对项目进行分组的某些方式。具体而言,二项式系数B(m,x)计算形成从n个不同项目的集合中选择的k个项目的无序集合的方式的数量。
二项式系数用于研究二项式分布和多分量冗余系统。它是由
例子 :
Compute B(7, 3) where m = 7 and x = 1
(7!/3!(7-3)!)7
= 7!/3!*4!
= (7*6*5*4*3*2*1)/(3*2*1)*(4*3*2*1)
= 35
需要一个二项式系数表来确定任何值m和x的二项式系数。
问题分析:
二项式系数可以递归计算如下:
更多,
也就是说,当x为零或m为零时,二项式系数为1。该程序将打印二项式系数表 。
C++
// C++ program for binomial coefficients
#include
// Function to print binomial table
int printbinomial(int max)
{
for (int m = 0; m <= max; m++) {
printf("%2d", m);
int binom = 1;
for (int x = 0; x <= m; x++) {
// B(m, x) is 1 if either m or x is
// is 0.
if (m != 0 && x != 0)
// Otherwise using recursive formula
// B(m, x) = B(m, x - 1) * (m - x + 1) / x
binom = binom * (m - x + 1) / x;
printf("%4d", binom);
}
printf("\n");
}
}
// Driver Function
int main()
{
int max = 10;
printbinomial(max);
return 0;
}
Java
// Java program for
// binomial coefficients
import java.io.*;
class GFG
{
// Function to print
// binomial table
static void printbinomial(int max)
{
for (int m = 0; m <= max; m++)
{
System.out.print(m + " ");
int binom = 1;
for (int x = 0; x <= m; x++)
{
// B(m, x) is 1 if either
// m or x is is 0.
if (m != 0 && x != 0)
// Otherwise using
// recursive formula
// B(m, x) = B(m, x - 1) *
// (m - x + 1) / x
binom = binom * (m - x + 1) / x;
System.out.print(binom + " ");
}
System.out.println();
}
}
// Driver Code
public static void main (String[] args)
{
int max = 10;
printbinomial(max);
}
}
// This code is contributed
// by akt_mit
Python3
# Python3 program for binomial
# coefficients
# Function to print binomial table
def printbinomial (max):
for m in range(max + 1):
print( '% 2d'% m, end = ' ')
binom = 1
for x in range(m + 1):
# B(m, x) is 1 if either m
# or x is is 0.
if m != 0 and x != 0:
# Otherwise using recursive
# formula
# B(m, x) = B(m, x - 1) *
# (m - x + 1) / x
binom = binom * (m - x + 1) / x
print( '% 4d'% binom, end = ' ')
print("\n", end = '')
# Driver Function
max = 10
printbinomial(max)
# This code is contributed by "Sharad_bhardwaj".
C#
// C# program for binomial coefficients
using System;
public class GFG {
// Function to print binomial table
static void printbinomial(int max)
{
for (int m = 0; m <= max; m++) {
Console.Write(m + " ");
int binom = 1;
for (int x = 0; x <= m; x++) {
// B(m, x) is 1 if either m
// or x is is 0.
if (m != 0 && x != 0)
// Otherwise using recursive formula
// B(m, x) = B(m, x - 1) * (m - x + 1) / x
binom = binom * (m - x + 1) / x;
Console.Write(binom + " ");
}
Console.WriteLine();
}
}
// Driver Function
static public void Main()
{
int max = 10;
printbinomial(max);
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
9 1 9 36 84 126 126 84 36 9 1
10 1 10 45 120 210 252 210 120 45 10 1