## CSDN博客

### Binomial Theorm 牛顿二项式定理(杨辉三角)

/*****************************************************************************************************
*  Binomial Theorm  杨辉三角
*  Author: Guangyun
*****************************************************************************************************
*/

#include <iostream>
#include <iomanip>
#include <cstddef>

using std::cout;
using std::endl;
using std::setw;

void binomial_theorem(size_t n)
{

//动态开辟内存
int **grid = new int*[n];
for(size_t i = 0; i != n; ++i)
{
grid[i] = new int[i + 1];
}

for(size_t i = 0; i != n; ++i)
{
grid[i][0] = 1;
grid[i][i] = 1;
}

for(size_t i = 2; i != n; ++i)
for(size_t j = 1; j != i; ++j)
grid[i][j] = grid[i - 1][j - 1] + grid[i - 1][j];

for(size_t i = 0; i != n; ++i)
{
for(size_t j = 0; j != i + 1; ++j)
{
cout << setw(5) << grid[i][j];
}
std::cout << endl;
}

//释放内存
for(size_t i = 0; i != n; ++i)
{
delete [] grid[i];
}
delete [] grid;
}

int main(int argc, char *argv[])
{
binomial_theorem(10);
return EXIT_SUCCESS;
}

0 0