Yang Hui triangle _ a good example of a two-dimensional array (reproduced)

Source: Internet
Author: User

Package com.test;
Import Java.util.Scanner;
public class Test {
public static void Main (string[] args)//Create an object to invoke, implement console input and output, because the input is a character, so a.show call the object when the emphasis on conversion.
{
Yanghui a = new Yanghui ();
Scanner s = new Scanner (system.in);
System.out.println ("Please enter a number to represent the number of rows:");
String str = s.nextline ();
A.show (integer.valueof (str));
}
}
Class yanghui//object calls the classes name
{
public void Show (int n)
{
Int[][] s = new Int[n + 1][n + 1];//defines a two-dimensional array because it is n rows, so define n+1 to prevent overflow (loop starting from 1)
for (int x = 1; x <= n × x + +)//This cycle is used to control the number of rows
{
for (int z = 1; z <= n + 1-x; z++)//This small loop is used to control the number of spaces in front of each line, and the more backward and misaligned, adjust the
{
System.out.print ("");
}
for (int y = 1; y <= x; y++)//This loop controls the number of rows per line
{
if (y = = 1)//fixed the value of the first number per line, and assigned a two-dimensional array, and printed
{
System.out.print (y + "");
S[x][y] = 1;
}
else if (y = = x)//fixed the value of the last number per line and assigned a two-dimensional array and printed
{
System.out.print (1 + "");
S[x][y] = 1;
}
The else//is used to print out the intermediate values of the first and last of each line and give the two-dimensional array
{
System.out.print (S[x-1][y-1] + s[x-1][y] + "");//The median value of each row is added by the s[x-1][y-1]+s[x-1][y of the previous row and given a two-dimensional array
S[x][y] = S[x-1][y-1] + s[x-1][y]; Record the value and prepare for the next line
}
}
System.out.println ();//Line break
}
}
}

Yang Hui triangle _ a good example of a two-dimensional array (reproduced)

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.