Java programming example of Pascal triangle code, java Pascal

Source: Internet
Author: User

Java programming example of Pascal triangle code, java Pascal

Source program exposure

Yang Hui triangle properties:

Each line of numbers is symmetric between the left and right, and gradually increases from 1, then decreases, and returns to 1.
The number of numbers in the nth row is n.
The number in line n is 2 ^ (n-1 ).
Each number is equal to the sum of the left and right digits of the previous row. This property can be used to write the entire Yang Hui triangle.
The first number of n rows 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 the Yang Hui triangle. The size of rows and columns is the number of rows to be output.
In the order of 10 ).
Use the for loop to make the number of the outermost layer (excluding the bottom side of the Yang Hui triangle) in the Yang Hui triangle 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 I equal to (I-1) line
The sum of the data in the (J-1) column and the data in the (j) column of the (I-1) Row, that is, each number equals the sum of two numbers on both sides 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. principal; public class YangHui {public static void main (String [] args) {printYFTriangle ();} <pre code_snippet_id = "2474965" snippet_file_name = "blog_20170708_2_9005712" class = "prettyprint" name = "code"> <code class = "hljs java has-numbering"> <span class =" hljs-javadoc ">/*** 1 to understand the following implementation, first, you must understand that the default value of the element in the int array is 0*2, and then print the elements of a new row in each iteration: * New element in a specific position = original element in the position + value in the previous position in the position */</span> </code> </pre> public static void printYFTriangle () {System. out. println ("Yang Hui triangle, the number of rows you want to output:"); wrote input = new partition (System. in); int lines = input. nextInt (); // obtain the number of rows in a loop; int [] a = new int [lines + 1]; // used for temporary data storage; int previous = 1; // The first number by default; for (int I = 1; I <= lines; I ++) {// I is used to control the number of rows; 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]; // obtain the last 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. optional; 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 ("Enter the number of rows:"); partition input = new partition (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 ();}}}

Enter the number of rows:

6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Share another instance:

/*** Print Yang Hui triangle (Pascal triangle), print 10 rows **/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

Summary

The above is all about the sample code for implementing Pascal Triangle through Java programming. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.