还记得中学时候学过的杨辉三角吗?以下是一个 66 行的杨辉三角:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
现在给你一个整数 n ,请你输出前 n 行的杨辉三角。
代码:
#include<bits/stdc++.h>
using namespace std;
int a[35][35];
int main()
{
int b,c,d,e,j;
while (cin >> b)
{
for (int i = 1; i<=b;i++)
{
a[i][1] = 1;
a[i][i] = 1;
}
for(int i=3;i<=b;i++)
for (int j=2;j<=i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
for (int i=1; i<= b;i++)
{
for (j=1;j<i;j++)
{
cout<<a[i][j]<<" ";
}
cout<<a[i][j]<<endl;
}
cout<<endl;
}
}