JAVA-using arrays and for loops to print 8 lines of Yang Hui triangle

Question : Write the program PrintRectangleOfYH.java using arrays and for loops to print 8 rows of Yang Hui triangles.
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
Solution:
1. Observe the graph first:
the first element in each row And the last element are 1;
other elements: for example, the second element in the fourth row is obtained by adding the second element in the third row and the first element in the third row.
2. The design algorithm
problem requires an array and for cycle. You can use a two-dimensional array to list the triangle shapes, and two for loops to traverse the array to assign values.

Insert picture description here
Note:
1. The nested use of two for loops distinguishes the functions of the outer for loop and the inner for loop: the
first for traversal is the row traversal of the two-dimensional array (the outer traversal) and the
second for traversal is Traverse the columns of each row (traversing the inner layer)
2. After completing an array traversal assignment, remember to wrap

Other questions are welcome to comment in the comment area, learn from each other and make progress together

Guess you like

Origin blog.csdn.net/weixin_45952057/article/details/109075418