Java small case--using a dual for loop to implement the output of the Yang Hui Triangle

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.