One-dimensional array implements the Yang Hui triangle, dimension Yang Hui triangle
Yang Hui triangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 43411 Accepted Submission (s): 18254
Problem Description Do you still remember the Yang Hui triangle I learned in middle school? The specific definition is not described here. You can refer to the following figure:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
The Input data contains multiple test instances. The Input of each test instance only contains a positive integer n (1 <=n <= 30), indicating the number of layers in the Yang Hui triangle to be output.
Output corresponds to each input. Please Output the Yang Hui triangle of the corresponding layers. Integers of each layer are separated by a space. An empty line is added after each Yang Hui triangle.
Sample Input
2 3
Sample Output
11 111 11 2 1
Authorlcy
SourceC language programming exercises (5)
Import java. util. optional; public class Main {static int [] mat = new int [31]; public static void main (String args []) {nation SC = new Nation (System. in); mat [0] = 1; // initialize the first element, that is, the values in the first column are all 1; int n = 0; while (SC. hasNext () {n = SC. nextInt (); triangle (n) ;}} public static void triangle (int n) {System. out. println (mat [0]); mat [n-1] = 1; // forward from the back to the front for (int I = 1; I <n; I ++) {// control the number of rows mat [I] = 1; for (int j = I-1; j> 0; j --) {mat [j] = mat [j] + mat [j-1]; if (mat [j] <0) {return ;}} for (int j = 0; j <= I; j ++) {if (j = 0) {System. out. print (mat [j]);} else {System. out. print ("" + mat [j]) ;}} System. out. println ();} System. out. println ();}}