Yang Hui Triangle

Source: Internet
Author: User

1. Problem description

The Yang Hui Triangle is printed according to the number of input rows, as shown in 1.10.

2. Problem analysis

Observe the pattern of the Yang Hui Triangle, you can find the law: the vertical edge of the triangle and the hypotenuse are "1", the triangle inside any number is exactly equal to its right above the number and the upper left corner of the number two digits of the sum. In the first few lines there are several numbers that can be added to the effect shown in 1.11.

Fig. 1.10 Yang Hui triangle figure 1.11 Phalanx

Phalanx (The matrix of equal rows and columns) Everyone is familiar with, can through two-dimensional array to deal with the square, a double loop can be achieved, the outer loop control the number of rows, the inner loop control column to complete the calculation and storage of the number in the square.

(1) Determine the program framework

From the previous problem analysis, we can first receive the height of the Yang Hui triangle from the keyboard, then calculate the Yang Hui triangle by the two-dimensional array, and finally print out the Yang Hui triangle. The program framework code is as follows:

public class Ch1_5

{

public static void Main (string[] args)

{

System.out.print ("Please enter the number of lines:");

Scanner Scanner = new Scanner (system.in);

int num = Scanner.nextint (); The number of rows received by the keyboard

int[][] ary = Gettriangle (num); Get Yang Hui Triangle

print (ARY); Print Yang Hui triangle

}

}

(2) Get Yang Hui triangle

From the previous problem analysis, the two-dimensional array to calculate the storage Yang Hui triangle, Yang Hui triangle vertical edge, hypotenuse are 1, you can assign a value, and then assign a value to the middle element, the value of the current position is equal to its top number and the sum of the upper left corner of the number. The program code is as follows:

private static int[][] gettriangle (int num)

{

int[][] ary = new Int[num][num]; Storage with two-dimensional arrays

for (int i = 0; i < ary.length; i++)//Vertical edge, bevel 1

{

Ary[i][0] = 1;

Ary[i][i] = 1;

}

for (int i = 1; i < ary.length; i++)//Outer loop control number of rows

{

for (int j = 1; J <= I; j + +)//inner Loop control column

{

The inside part is equal to the sum of the upper and upper left corners of the current position

ARY[I][J] = Ary[i-1][j-1] + ary[i-1][j];

}

}

return ary;

}

(3) Print Yang Hui triangle

The Yang Hui triangle is stored in a two-dimensional array, which can be printed through a double loop, but it is important to note that all elements are not printed out, and the control of the inner loop column is less than or equal to the current number of rows. The program code is as follows:

private static void print (int[][] ary)

{

for (int i=0;i<ary.length;i++)//Outer loop control line

{

for (int j=0;j<=i;j++)//Inner Loop control column

{

System.out.printf ("%-3d", Ary[i][j]);

}

System.out.println (); Line break

}

}

(4) Complete program

Now we need to combine the procedures we have just made to form our complete program:

Import Java.util.Scanner;

public class Ch1_5

{

public static void Main (string[] args)

{

System.out.print ("Please enter the number of lines:");

Scanner Scanner = new Scanner (system.in);

int num = Scanner.nextint (); Number of rows received from the keyboard

int[][] ary = Gettriangle (num); Get Yang Hui Triangle

print (ARY); Print Yang Hui triangle

}

Get Yang Hui Triangle

private static int[][] gettriangle (int num)

{

int[][] ary = new Int[num][num]; Storage with two-dimensional arrays

for (int i = 0; i < ary.length; i++)//Vertical edge, bevel 1

{

Ary[i][0] = 1;

Ary[i][i] = 1;

}

for (int i = 1; i < ary.length; i++)//Outer loop control line

{

for (int j = 1; J <= I; j + +)//inner Loop control column

{

The inside part is equal to the sum of the upper and upper left corners of the current position

ARY[I][J] = Ary[i-1][j-1] + ary[i-1][j];

}

}

return ary;

}

private static void print (int[][] ary)

{

for (int i=0;i<ary.length;i++)//Outer loop control line

{

for (int j=0;j<=i;j++)//Inner Loop control column

{

System.out.printf ("%-3d", Ary[i][j]);

}

System.out.println (); Output line break

}

}

}

(5) Operation result

Run the program, as shown in result 1.12.

Figure 1.12 Program Output results

3. Extended Training

Figure 1.12 Output of the Yang Hui Triangle is right triangle, can output isosceles triangle it? The answer is yes. Isosceles triangle similar to the previous pyramid pattern, referring to the idea described earlier, it is not difficult to output isosceles triangle.

(1) Reference code

Import java.util.*;

public class Ch1_5_2

{

public static void Main (string[] args)

{

Scanner in = new Scanner (system.in); Get console input Object

System.out.print ("Please enter line number:");

int m = In.nextint (); Receiving input from the keyboard

int n=2*m-1; Number of column elements

int arr[][]=new Int[m][n];

for (int i=0;i<m;i++)//Outer loop control line

{

for (int j=0;j<n;j++)//Inner Loop control column

{

if (j< (m-i-1) | |    (j>= (m+i))) Output isosceles triangle space on both sides

System.out.print ("");

else if ((j== (m-i-1)) | | (j== (m+i-1)))
Calculate and output isosceles triangle two waist

{

Arr[i][j]=1;

System.out.printf ("%-3d", Arr[i][j]);

}

ElseIf ((i+j)%2==0&&m%2==0| | (i+j)%2==1&&m%2==1)
Middle Default number 0 is replaced with a space

System.out.print ("");

else//calculate and output intermediate numbers

{

ARR[I][J]=ARR[I-1][J-1]+ARR[I-1][J+1];

System.out.printf ("%-3d", Arr[i][j]);

}

}

System.out.println (); Output line break

}

}

}

(2) Operation result

Run the program, as shown in result 1.13.

Figure 1.13 Program Output results

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.