[Algorithm] Binomial Theorem Printing Yang Hui Triangle

topic:

Input a number, without using an array or dynamic space, print out the Yanghui triangle corresponding to this number, multiple input

Ideas:

According to the binomial theorem, the number of m columns in the nth row of Yang Hui's triangle (n, m starts from 0), corresponds to the mth coefficient of the nth quadratic expansion

即:arr[n][m] = n! / (m! * (n-m)!)

source code:

#include <iostream>
using namespace std;

int factorial(int n)
{
	int res = 1;
	for (int i = 1; i <= n; ++i)
	{
		res *= i;
	}
	return res;
}

int main()
{
	int n;
	while (cin >> n)
	{
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j <= i; ++j)
			{
				cout << factorial(i) / (factorial(j) * factorial(i - j)) << " ";
			}
			cout << endl;
		}
		cout << endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/phoenixFlyzzz/article/details/130436596