Enter the number of lines to display the Yang Hui's triangle, will print out the pyramid-type Yang Hui's triangle, but too many lines, the effect is not very good, you can adjust the format control
Copy code code as follows:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i,j,k;
int Li Ne
int *prev, *next;
printf ("Enter the number of rows to view the Yang Hui's triangle (greater than 2):");
scanf ("%d", &line);
if (Line < 2)
{
printf ("Number of rows is less than 2,goodbye!n");
exit (1);
for (i=1 i<=line; i++) //The first two lines of printing
printf (" ");
printf ("%6dn", 1);
for (i=1 i<=line-1; i++)
printf (" ");
printf ("%6d%6dn", 1, 1);
prev = malloc (2*sizeof (int));
prev[0] = 1;
prev[1] = 1;
for (i=3 i<=line; i++) //print from the third line
{
next = malloc (i*sizeof ( int));
next[0] = 1;
next[i-1] = 1;
for (j=line j>=i; j--) //outer Space
{
printf (" ");
&nbsP;&NBSP}
printf ("%6d", 1);
for (k=2; k<i; k++) //number
{
next[k-1] = prev[k-2] + prev[k-1];
printf ("%6d", next[k-1) ;
}
}
printf ("%6dn", 1);
free (prev);
prev = next;
}
free (next);
return 0;
}