Print out the following Yang Hui's triangles (10 lines are required to print)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 1
............
Required to print out 10 lines
Yang Hui's triangle: The end point is 1, each number equals the sum of two digits on its shoulder.
#include "stdafx.h"
#include <iostream>
using namespace Std;
int _tmain (int argc, _tchar* argv[])
{
int array[10][10]; Defines a 10*10 array of two dimensions
int i,j;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
array[i][j]=0; Assigns an initial value so that each element of the array is 0
}
}
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
if (j==0)
{
Array[i][j]=1; Make each number 1 on the first column
}
}
}
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
if (j>0&&i>0&&j<=i)//Here J is required to be less than or equal to I so that you can only perform the required operation in the triangle
{
ARRAY[I][J]=ARRAY[I-1][J-1]+ARRAY[I-1][J]; The characteristic of a Yang Hui's triangle is equal to the sum of two numbers on its shoulders, i.e. array[i][j]=array[i-1][j-1]+array[i-1][j];
}
}
}
for (i=0;i<10;i++)
{
for (j=0;j<=i;j++)//output only triangles, not entire matrices
{
cout<<array[i][j]<< "";
}
cout<<endl; Once you have finished outputting a line, you can wrap it.
}
return 0;
}