C language printing Yang hui triangle example summary

  • 2020-05-09 18:54:40
  • OfStack

Yang hui 3 jiao is what we have known since junior high school. Now, let's use C language to display it on the computer.

In junior high school, we know, Yang hui 3 Angle of the two waist are 1, the number of other positions are the sum of the top two Numbers. This is the key to writing Yang hui jiao 3 in C. In high school, we also know that any 1 row of Yang hui's Angle 3 is a coefficient of 2 terms, and n is the number of rows minus 1. That means that any number of 1 is equal to this is the number of combinations in high school. n is the number of rows minus 1, not the number of columns minus 1. For example, the third number in line 5 is equal to 6.

Now let's do it the first way: define a 2-dimensional array: a[N][N], which is slightly larger than the number of lines to print. Let's make both sides 1, that is, when the first number and the last number in each row are 1. a[i][0]=a[i][i-1]=1, n is the number of rows. Any number other than those on either side is the sum of the top two digits, a[i][j]=a[i-1][j-1]+a[i-1][j]. Finally output Yang hui 3 jiao. The code is as follows:


#include <stdio.h>
#define N 14
void main()
{
  int i, j, k, n=0, a[N][N]; /* define 2 Dimensional array a[14][14]*/
  while(n<=0||n>=13){ /* Control the number of printed lines is not too large, too much will cause display irregularities */
    printf(" Please enter the number of lines to print: ");
    scanf("%d",&n);
  }
  printf("%d Line of Yang hui 3 Angle is as follows: \n",n);
  for(i=1;i<=n;i++)
    a[i][1] = a[i][i] = 1; /* Let's call both sides zero 1 Because now loop from 1 From the beginning, think a[i][1] For the first 1 The number of */
  for(i=3;i<=n;i++)
    for(j=2;j<=i-1;j++)
      a[i][j]=a[i-1][j-1]+a[i-1][j]; /* Everything except for both sides is equal to the sum of the two ends */ 
  for(i=1;i<=n;i++){
    for(k=1;k<=n-i;k++)
      printf("  "); /* this 1 The line is basically a space holder before the output number to make the output number more beautiful */
    for(j=1;j<=i;j++) /*j<=i The reason is not to output other Numbers, only the number we want to output */
      printf("%6d",a[i][j]);
    
    printf("\n"); /* when 1 When the line output is finished, the line feed continues 1 The output */
  }
  printf("\n");
}

Operation results:
Please enter the number of lines to print: 10
The 10 lines of Yang hui's 3 corners are as follows:


                1
               1   1
             1   2   1
            1   3   3   1
          1   4   6   4   1
         1   5  10  10   5   1
       1   6  15  20  15   6   1
      1   7  21  35  35  21   7   1
    1   8  28  56  70  56  28   8   1
   1   9  36  84  126  126  84  36   9   1

We use a 2-dimensional array for the 1 method above, and we use a custom function for the 1 method below.

In high school, we know that any number in Yang hui 3 is equal to a combination number. Now let's use this formula to do it. First of all, the code of this method is as follows:


#include <stdio.h>
/* 
 *  Define the factorial, you might think about it here. Why? float When I try the first 1 The next time, 
 *  If you use int If you print too many lines, you will get an error. 
 *  And that's because the factorial is a larger number int Not enough. The same below 
 */
float J(int i){
  int j;
  float k=1;
  for(j=1;j<=i;j++)
    k=k*j;
  return(k);
}
float C(int i,int j){ /* Define the number of combinations */
  float k;
  k=J(j)/(J(i)*J(j-i));
  return(k);
}
void main(){
  int i=0,j,k,n; /* Print Yang hui 3 Angle */ 
  while(i<=0||i>16){
    printf(" Please enter the number of lines to print: ");
    scanf("%d",&i);
  }
  printf("%d Line of Yang hui 3 Angle is as follows: \n",i);
  for(j=0;j<i;j++){
    for(k=1;k<=(i-j);k++)
      printf(" ");
    for(n=0;n<=j;n++)
      printf("%4.0f",C(n,j));
    printf("\n");
  }
  printf("\n\n");
}

Operation results:
Please enter the number of lines to print: 10
The 10 lines of Yang hui's 3 corners are as follows:


            1
           1  1
          1  2  1
         1  3  3  1
        1  4  6  4  1
       1  5 10 10  5  1
      1  6 15 20 15  6  1
     1  7 21 35 35 21  7  1
    1  8 28 56 70 56 28  8  1
   1  9 36 84 126 126 84 36  9  1

The idea is to know the representation of the number of combinations. And if you define a custom function. However, this method is not recommended because it produces large amounts of data.


Related articles: