# Contains &; stdio.h & ampgt;
# Definition number 10
void setdata(int(*s)[N],int n){
int i,j;
for(I = 0; I & n; I++)// The elements in the first column and diagonal are set to 1.
{
Standard & Poor's. lt; I > & amplt; I > = 1; Standard & Poor's. lt; I > [0]= 1;
}
for(I = 2; I & n; I++){// Assign numbers to other elements of Yang Hui's triangle.
for(j = 1; Johnson & Johnson company lt; Me; j++){
Standard & Poor's. lt; I > [j]= s[I- 1][j- 1]+s[I- 1][j];
}
}
}
void outdata(int s[][N],int n){
int i,j;
Printf ("Yang Hui Triangle: \ n"); //Only the lower half triangle of the matrix is output.
for(I = 0; I & n; i++){
for(j = 0; Johnson & Johnson company lt; = I; j++){
printf("%6d ",s & amplt; I > [j]);
}
printf(" \ n ");
}
}
main(){
int y[N][N],N = 7;
setdata(y,n); //Set the number of array elements according to the law.
outdata(y,n); //Output Yang Hui Triangle
}
You can put the value of Yang Hui's triangle in the lower half of a square. If you want to print a seven-line Yang Hui triangle, define a square matrix equal to or greater than 7x7, but do not use the top half and the rest of the matrix.
Yang Hui Triangle has the following characteristics:
(1) The elements in the first column and diagonal are1;
(2) Except for the elements in the first column and diagonal, the values of other elements are the sum of the elements in the same column in the previous row and the elements in the previous column.
Method two,
# Contains &; stdio.h & ampgt;
# Define Line 10
int main()
{
int arr[LINE][LINE]= { 0 };
int I = 0;
int j = 0;
for(I = 0; I & Line; i++)
{
arr & amplt; I > [0]= 1;
arr & amplt; I > & amplt; I > = 1;
}
for(I = 2; I & Line; i++)
{
for(j = 1; Johnson & Johnson company lt; Me; j++)
{
arr & amplt; I > [j]= arr[I- 1][j- 1]+arr[I- 1][j];
}
}
for(I = 0; I & Line; i++)
{
for(j = 0; Johnson & Johnson company lt; (2 * LINE-2 * I); j++)
{
printf(" ");
}
for(j = 0; Johnson & Johnson company lt; = I; j++)
{
printf("%4d ",arr & amplt; I > [j]);
}
printf(" \ n ");
}
Returns 0;
}
Method three,
# define _ CRT _ SECURE _ NO _ WARNINGS 1
# Contains &; stdio.h & ampgt;
int main()
{
int i,j,n = 0;
//First, define the counting symbols I and J of two-dimensional array, and initialize the number of rows of Yang Hui triangle.
int a[ 100][ 100]={0, 1 };
//There are only two initial values, namely a[0][0]= 1, a[0][ 1]=2, and the initial values of other array elements are all 0.
//The size of the two-dimensional array can be customized, but remember not to exceed the size of the plastic array.
And (n & 1。 gt; 100)
//Automatically initialize the question when the input value is incorrect, and re-enter.
{
Printf ("Please enter the number of triangular lines of Yang Hui to print &; gt; :");
scanf("%d ",& ampn);
}
for(I = 1; I & n; I++)// The first cycle I decides which line.
{
for(j = 1; Johnson & Johnson company lt; = I; J++)// The second layer circularly borrows I to limit the number of characters per line.
{
a & amplt; I > [j]= a[I- 1][j- 1]+a[I- 1][j];
}
}
for(I = 1; I & n; I++)//for loop prints a two-dimensional array line by line.
{
for(j = 1; Johnson & Johnson company lt; = I; j++)
printf("%5d ",a & amplt; I > [j]);
printf(" \ n ");
}
Returns 0;