Keywords:
| Basic practice two-dimensional arrays |
Topic:
Workaround:
1 PackageCom.algorithm.java.blueBirdge;2 3 ImportJava.util.Scanner;4 5 Public classTest {6 Public Static voidMain (string[] args) {7 //fetching rows from the console8Scanner s =NewScanner (system.in);9 introw =s.nextint ();Ten //A two-dimensional array is defined according to the number of rows, because the number of elements in each row is different, so the number of each row is undefined One int[] arr =New int[row][]; A //traversing a two-dimensional array - for(inti = 0; i < row; i++){ - //initializes this one-dimensional array of each row theArr[i] =New int[i + 1]; - //iterate through this one-dimensional array and add elements - for(intj = 0; J <= I; J + +){ - //The beginning and ending elements of each column are 1, at the beginning of the j=0, at the end, j=i + if(j = = 0 | | j = =i) { -ARR[I][J] = 1; +}Else{//each element is the sum of the elements of its previous line and the diagonal elements AARR[I][J] = Arr[i-1][j] + arr[i-1][j-1]; at } -System.out.print (Arr[i][j] + "\ T"); - } - System.out.println (); - } - } in -}
Test results:
Test success!
Java Blue Bridge Cup-basic exercise (5) Yang Hui triangle