C-language print Yang Hui's triangle example Rollup _c language

Source: Internet
Author: User

Yang Hui's triangle is we know from Junior high School, now, let us use C language to display it on the computer.

In junior high, we know that the two waist of the Yang Hui's triangle is 1, and the number of other positions is the sum of the top two numbers. This is one of the keys to writing the Yang Hui's triangle in C language. In high school we know that any line in the Yang Hui's triangle is a two-item coefficient, and n is a row number minus 1. Which means that any number equals this is the combination number of high school. n represents the number of rows minus 1, not the number of columns minus 1. For example, the third number on line fifth is = 6.

Now we write the first idea: Define a two-dimensional array: a[n][n], slightly larger than the number of lines to print. Then make the numbers on both sides 1, that is, when the first and last digits of each row are 1. A[i][0]=a[i][i-1]=1,n is the number of rows. In addition to the numbers on either side, any number is the sum of the two topness, that is, a[i][j]=a[i-1][j-1]+a[i-1][j]. The final output Yang Hui's triangle. The code is as follows:

#include <stdio.h>
#define N "
{
  int i, J, K, N=0, a[n][n];/* Defines a two-dimensional array a[14][14]*/
  while (n<=0| | N>=13) {/* control the number of lines printed not too large, the General Assembly caused by the display of irregular
    /printf ("Please enter the number of lines to print:");
    scanf ("%d", &n);
  }
  printf ("%d rows Yang Hui's triangle is as follows: \ n", n);
  for (i=1;i<=n;i++)
    a[i][1] = a[i][i] = 1;/* The number on both sides makes it 1, because now the loop starts at 1, it thinks A[i][1] is the first number * * 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];/* is equal to the sum of the two topness except the number on both sides * * for 
  (i=1;i<=n;i++) { For
    (k=1;k<=n-i;k++)
      printf ("  ");/* This line is primarily to hit the space placeholder before the output number, so that the number of output is more beautiful. * for
    (j=1;j<=i;j++)/* The reason for j<=i is not to output other numbers, only output the number we want
      /printf ("%6d", A[i][j]);
    
    printf ("\ n"); * * To continue the next line of output after line output/
  }
  printf ("\ n");
}

Run Result:
Please enter the number of lines to print: 10
10 lines Yang Hui's triangle as follows:

                1
               1   1
             1   2   1  1 3 3 1 1 4 6 4 1 1-5   5   1
       1 6 6   1  1 7/35   7   1
    1 8 for   8
   1 1   9  126  126   9   1

The above method we use the two-dimensional array, the following method we will use the custom function.

In high school we know that any number in the Yang Hui's triangle is equal to a combination number, and now we use this formula to do it. First, this method code is as follows:

#include <stdio.h>
* 
 * define factorial, which may be thought of here. Why to use float, when I try the first time,
 * If you use int, then the number of printed lines after the error.
 * This is because the number of factorial is relatively large, if the use of int is not enough. The same as
 * *
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) {/* Defines 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's triangle 
  /while i<=0| | i>16) {
    printf ("Enter the number of lines to print:");
    scanf ("%d", &i);
  }
  printf ("%d rows Yang Hui's triangle 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 \ nthe");
}

Run Result:
Please enter the number of lines to print: 10
10 lines Yang Hui's triangle 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-
    6 1 1 7  (7) 1 1 8 28
   8  1
   1  9 126 126  9  1

This method is mainly to know the representation of the combination number. And if you have a custom function. However, the data produced by this method is relatively large, and this method is not recommended.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.