The most standard Yang Hui Triangle and the general Yang Hui Triangle

Source: Internet
Author: User
Tags control characters

1, the most standard Yang Hui triangle

/* Write program print Yang Hui triangle 1             variant 1 0 0 1 1 1 0 2 1   1 3 3 1 idea: We define a two-dimensional array, all elements are first initialized to 0 to the 1th column of the array and the diagonal element is assigned 1 of the rest of the elements a[i][j]=a[i-1][j-1]+a[i-1][j] output This two-dimensional array of the lower triangular *///Yang Hui triangle so standard # include<stdio.h> #include <stdlib.h> #define N 5int Main () {int a[n][n]={0};//all 0//Initializes a two-dimensional array for (int i=0;i< n;i++) {for (int j=0;j<n;j++) {if (j==0 | | i==j)//note is j==0 instead of I==0 {a[i][j]=1;//The following two sentences are equivalent to the above//a[i][i]=1;//a[i][0]=1;} ELSE{A[I][J]=A[I-1][J-1]+A[I-1][J];} printf ("%-8d", A[i][j]);//Direct Edge Assignment Edge printing}//printf ("\ n"); } for   (int i=0;i<n;i++) {printf ("%*d", 30-i*2,a[i][0]);//Print the first column, * * *, here must be half of the control characters below in order to fall in the middle of the position, Represents the first digit of each row and the leading interval character of the line  //The step must be half below to fall in the middle, where 30 is a random number/* 1                  1 0 0 1 1 1 0      1< 2 C15/>1 2 1 0*/for (int j=1;j<=i;j++)//The reason for looping from the J=1, because the first column has been output, to extract control of each line and the beginning of the interval  {printf ("%4d", A[i][j]);//% 4d represents the width of 4 characters, if this is changed to%6d, then the above is changed to 30-i*3} printf ("\ n");   }   GetChar ();  System ("Pause");}


Second, the general Yang Hui Triangle

/* Write program print Yang Hui triangle 1             variant 1 0 0 1 1 1 0 2 1   1 3 3 1 idea: We define a two-dimensional array, all elements are initialized to 0 to the 1th column of the array and the diagonal element is assigned a value of 1 of the remaining elements A[i][j]=a[i-1][j-1]+a[i-1][j] output This two-dimensional array of the lower triangle */#include < Stdio.h>int Main () {int a[4][4]={0};p rintf ("Initialize all to 0\n"), for (int. i=0;i<4;i++) {for (int j=0;j<4;j++) {printf (" %-6D ", A[i][j]);} printf ("\ n");} for (int i=0;i<4;i++) {for (int j=0;j<4;j++) {if (I==j | | j==0) {a[i][j]=1;//The following two sentences are equivalent to the above//a[i][i]=1;//a[i][0]=1;} ELSE{A[I][J]=A[I-1][J-1]+A[I-1][J];}}} Output Yang Hui triangle for (int i=0;i<4;i++) {for (int j=0;j<=i;j++)//j<4 The result that the previously assigned value of 0 has changed to random number {printf ("%-6d", A[i][j]);} printf ("\ n");} return 0;}


The most standard Yang Hui Triangle and the general Yang Hui Triangle

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.