Yang Hui triangle Feature analysis ():
* Line i has column I
* The first number of each row is 1
* The last number of each row is 1
* The current number (not the first column and the last column) equals the number above a number + the left of the previous number
Implementation code:
/*** Requirements: output Yang Hui triangle *@authorAdministration **/ Public classYanghuitest { Public Static voidMain (string[] args) {//Create a two-dimensional array, define rows, no columns defined int[] arr =New int[10][]; //dynamically creates space for a column (the number of columns per row in the Yang Hui Triangle is the same as the current line number, such as: 5th row has 5 columns) for(inti = 0; i < arr.length; i++) {Arr[i]=New int[I+1]; } //Assignment Operation for(inti = 0; i < arr.length; i++) {arr[i][0] = 1;//The 1th column of row I, that is, the first column of each row is 1Arr[i][i] = 1;//column I of row I, that is, the last number of each row is 1//Next is the core of the Yang Hui Triangle. for(intj=1;j<i;j++) {//Note that the J here needs to be counted from 1, because the first number of each row has been assigned.//The current value--the number of the column on the previous row + the first number on the left of the column on the previous line (see image)Arr[i][j]=arr[i-1][j]+arr[i-1][j-1]; } } //Print Output for(inti = 0; i < arr.length; i++) { for(intj = 0; J < Arr[i].length; J + +) {System.out.print (Arr[i][j]+ "\ T"); No Line break} System.out.println (); Line Change}}}
Operation Result:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 5 1 1 6, 6 1 1 7 7 1 1 8 8 1 1 9 126 126 9 1
Java small case--using a dual for loop to implement the output of the Yang Hui Triangle