Source program reveals
Properties of Yang Hui’s triangle:
Each row of numbers is symmetrical left and right, starting from 1 gradually becomes larger, then becoming smaller, and returning to 1.
The number of numbers in line n is n.
The sum of the numbers on line n is 2^(n-1).
Each number is equal to the sum of the left and right numbers of the previous line. This property can be used to write the entire Yang Hui triangle.
The first number in line n is 1, the second number is 1× (n-1), the third number is 1× (n-1) × (n-2) /2, and the fourth number is
1× (n-1) × (n-2) /2× (n-3) /3…and so on.
Algorithm Principle 1:
Use a two-dimensional array yh[][] to store the data of Yang Hui triangle. The size of the rows and columns is the number of rows that need to be output (Row is 10 in this program).
Use the for loop to make the number except the outermost layer (excluding the bottom edge of the Yang Hui triangle) in the Yang Hui triangle is 1;
Use the statement yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j] to make the data in column j of row j equal to the sum of the data in column (j-1) of row (i-1) and column (j) of row (i-1), that is, each number is equal to the sum of the two left and right numbers of the previous row.
package com.work; public class YangHuiSanJiao { public static void main(String[] args) { int [][]a = new int [10][10]; for(int n = 0; n < 10;n++) { a[n][0] = 1; a[n][n] = 1; } for(int n = 2; n <10; n++) { for(int j = 1; j < n; j++) { a[n][j] = a[n -1][j -1] + a[n - 1][j]; } } for(int n = 0; n < 10; n++) { for(int k = 0; k < 2 * (10 - n) - 1; k++) { System.out.print(" "); } for(int j = 0; j <= n; j++) { System.out.print(a[n][j] + " "); } System.out.println(); } } }Method 2
package com.face; import java.util.Scanner; public class YangHui { public static void main(String[] args) { printYFTriangle(); } <pre code_snippet_id="2474965" snippet_file_name="blog_20170708_2_9005712" name="code"><code><span>/** * 1 To understand the following implementation, first you must understand that the default value of elements in the int array is 0 * 2 Then every time you iterate to print the element of a new line: * New element in a specific position = the original element in that position + The value of the previous position of this position*/</span></code></pre>public static void printYFTriangle(){ System.out.println("Yang Hui triangle, the number of lines you are preparing to output:"); Scanner input = new Scanner(System.in); int lines = input.nextInt();//Get the number of lines of the loop; int[] a = new int[lines + 1];//Temporarily store data; int previous = 1; //Default first number; for (int i = 1; i <= lines; i ++){//i is used to control the number of lines; for(int j=1;j<=lines-i;j++){//Output space, very easy; System.out.print(" "); } for (int j = 1; j <= i; j++){ int current = a[j];//Get the next number first, a[j] = previous + current; previous = current; System.out.print(a[j] + " "); } System.out.println(); }}}Method 3: Recursive implementation
package com.face; import java.util.Scanner; public class DiGui { static int fun(int n,int k){ //n, row, k:column if(k==1||n==k) return 1; else return fun(n-1,k-1)+fun(n-1,k); } public static void main(String[] args) { int lines; System.out.println("Please enter the number of lines:"); Scanner input=new Scanner(System.in); lines=input.nextInt(); for(int i=1;i<=lines;i++){ for(int k=1;k<lines-i+1;k++){ System.out.print(" "); } for(int j=1;j<=i;j++){ System.out.print(fun(i,j)+" "); } System.out.println(); } } }Please enter the number of lines:
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Let's share another example:
/** * Print Yanghui triangle (Pascal triangle), print 10 lines* */public class Yanghuisanjiao { public static void main(String[] args) { int [][] a = new int[11][11]; for (int i = 0 ; i < 10 ; i++) { a[i][0] = 1; a[i][i] = 1; } for (int i = 1 ; i < 10 ; i ++) { for (int j = 1; j < i ; j++) { a[i][j] = a[i-1][j-1] + a[i-1][j]; } } for (int i = 0; i < 10 ; i++) { for (int j = 0; j < 10-i;j++) { System.out.print(" "); } for (int k = 0; k < 10;k++) { if (a[i][k] != 0) { System.out.print(a[i][k]+" "); } } System.out.println(); } } }result:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Summarize
The above is all about the examples of Java programming to implement Pascal triangle code, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!