Prints a 10-row Yang Hui triangle using a two-dimensional array.

Source: Internet
Author: User

/
 * * Use a two-dimensional array to print a  10-row Yang Hui triangle.
		1
		1 1
		1 2 1
		1 3 3 1
		1 4 6 4 1
		1 5 ten 5 1
		....
"Hint"
	1. The first line has 1 elements, and the nth row has n elements
	2. The first and last elements of each row are 1
	3. Starting with the third row, for elements that are not the first and last element. 
	YANGHUI[I][J] = Yanghui[i-1][j-1] + yanghui[i-1][j]; 
 *
/public class Test_03yanghui {public
	static void Main (string[] args) {
		//1. Create a two-dimensional array of the specified format
		int[][] Yanghui = new int[10][];
		for (int i = 0; i < yanghui.length; i++) {
			Yanghui[i] = new int[i+1];
		}
		
		2. Assign a value for the two-dimensional array for
		(int i = 0; i < yanghui.length; i++) {
			yanghui[i][0] = yanghui[i][i] = 1;
			if (i > 1) {for
				(int j = 1; j < Yanghui[i].length-1; J + +) {
					yanghui[i][j] = yanghui[i-1][j-1] + Yan GHUI[I-1][J];}}
		}
		3. Traverse the two-dimensional array for
		(int i = 0; i < yanghui.length; i++) {for
			(int j = 0; J < Yanghui[i].length; J + +) {
				S Ystem.out.print (Yanghui[i][j] + "");
			}
			System.out.println ();}}}


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.