Prints the first 10 lines of the Yang Hui Triangle. Yang Hui triangles such as: 1 1 1 1 1 1 1 2 1 1 2 1 1 3 3 1 1 3 3 1 1 4 6 4 1 1 4 6 4 1 [Figure 5-1] [Figure 5-2] "Problem analysis" observation figure 5-1, it is not easy to find the law, but if it is converted to figure 5-2, it is not difficult to find that the Yang Hui triangle is actually a small triangular part of a two-dimensional table, assuming that through a two-dimensional array yh storage, each row and the end of the element is 1, and the value of any non-first element yh[i][j] is actually the sum of yh[i-1][j-1] and Yh[i-1][j], and the number of elements in each row is exactly equal to the number of rows. With the value of the array element, to print the Yang Hui Triangle, just control the output starting position.
#include <iostream>
#include <iomanip>
using namespace Std;
int a[11][11];
int main ()
{
int i,j;
A[1][1]=1;
for (int i=2;i<=10;++i)
{
A[i][1]=1;a[i][i]=1;
for (int j=2;j<=i-1;++j)
A[I][J]=A[I-1][J-1]+A[I-1][J];
}
for (int i=1;i<=10;i++)
{
if (i!=10) COUT<<SETW (30-3*i) << "";
for (int j=1;j<=i;j++)
COUT<<SETW (6) <<a[i][j];
cout<<endl;
}
return 0;
}
Prints the first 10 lines of the Yang Hui Triangle.