题目描述
打印杨辉三角形的前n行。 当n=5时,输出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
输入
输入行数�n。
输出
输出如题述三角形。 n行,每行各数之间用一个空格隔开。
样例输入
5
样例输出
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
代码
#include<bits/stdc++.h>
using namespace std;
const int N=21,M=21;
int a[N][M];
int main()
{
int n;
cin>>n;
a[1][1]=1;
cout<<a[1][1];
cout<<endl;
for (int i=2;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}