[アルゴリズム] 二項定理印刷陽輝三角形

トピック:

配列または動的スペースを使用せずに数値を入力し、この数値に対応する陽輝三角形を出力し、複数入力します。

アイデア:

二項定理によると、陽輝の三角形 (n、m は 0 から始まる) の n 行目の m 列の数は、n 番目の二次展開の m 番目の係数に対応します。

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

ソースコード:

#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;
}

おすすめ

転載: blog.csdn.net/phoenixFlyzzz/article/details/130436596