杨辉三角形的代码

#include
using namespace std;
int a[15][15];

int main()//规律:a[i][j] = a[i-1][j]+a[i-1][j-1]
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	if (n <= 1 || n >= 10) return 0;
	a[0][0] = 1;//数组从下标一开始,这样a[i][0]的值就不用再另外赋值了
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= i; ++j) a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= i; ++j)if (a[i][j] != 0) cout << a[i][j] << " \n"[j == i];
	}
	return 0;
}

你可能感兴趣的:(算法,c++)