How to print Yang Hui Triangle with C language
This article is about how to print Yang Hui Triangle in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Topic description
Print Yang Hui Triangle (first N lines)
Analysis of problems
Yang Hui triangle is one of the outstanding research achievements of ancient Chinese mathematics. It graphically reflects some inherent algebraic properties of combinatorial numbers and is a discrete combination of numbers and shapes.
Some of the laws of Yang Hui's triangle:
Each number is equal to the sum of the two numbers above it.
Each line of numbers is symmetrical and increases gradually from 1 to 1.
The number in the nth line has n items.
The m number of the nth line can be expressed as C (nmur1mai 1), that is, the combination number of one element taken from one different element of nmuri.
According to the first three laws, we can use the array method to obtain the Yang Hui triangle; according to the latter two laws, we can use the formula method to find out the numbers of each row and column.
The idea of array method: first define a two-dimensional array according to the set number of rows, and then use a double-layer loop. The factor of the outer loop is the number of rows of the Yang Hui triangle, and the inner loop is used to store the numbers of each row of the Yang Hui triangle into the array. The first and last columns of each row are 1, and the number in the middle is equal to the sum of the two numbers above it.
Finally, the numbers in the two-dimensional array are printed through a two-layer loop.
The idea of formula method: because the Yang Hui triangle satisfies the fourth rule mentioned above, we can directly define a function to calculate the m number of the n rows of the Yang Hui triangle.
Combination number formula
According to the formula of the above combination, we can use factorial and related calculations to find each number of Yang Hui's triangle and print it out at the same time.
1. Use the array method (print right triangles)
Print a right-angled Yang Hui triangle, that is, print a two-dimensional array without spaces.
Code
# include # define LINE_MAXIMUM 10 / / number of rows int main () {int I = 0, j = 0; int array [line _ MAXIMUM] [LINE_MAXIMUM] = {0}; / * fill two-dimensional array * / for (I = 0; I < LINE_MAXIMUM; iTunes +) / / number of rows {for (j = 0; j