English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java programming implements two output results of Pascal's triangle example code

Firstly, let's show the results:

Introduction:

Yang Hui's triangle is a geometric arrangement of binomial coefficients in a triangle. In Europe, this table is called Pascal's triangle. Pascal (1623----1662) is located at1654year, later than Yang Hui in discovering this law393year, later than Jia Xian6In the year 00, Yang Hui's triangle is one of the outstanding research achievements of ancient Chinese mathematics. It graphically represents the binomial coefficients and intuitively embodies some algebraic properties of combinations within the figure, which is a beautiful combination of discrete numbers and shapes.

Example code is as follows:

package com.sxt;
import java.util.Arrays;
public class KeBen {
	public static void main(String[] args) {
		int[][] array =new int []10][10];
		array [0]=new int[]{1};
		//The first row is1
		for (int i=1;i<10;i++{
			array[i]=new int [i+1];
			for (int j=0;j<i+1;j++{
				if(j==0||j==i){
					//Boundary special treatment
					array[i][j]=1;
				}
					//Equal to the sum of the two shoulders of the previous row
					array[i][j]=array[i-1][j]+array[i-1][j-1];
				}
			}
		}
		//Simple output
		for (int i=0;i<10;i++{
			System.out.println(Arrays.toString(array[i]));
		}
		//Layout output
		for (int i=0;i<10;i++{
			for (int j=0;j<10-i-1;j++{
				System.out.print(" ");
				//Two spaces
			}
			for (int j=0;j<=i;j++{
				String a=""+array[i][j];
				//Convert to string
				//Consider separately when the string length is different
				if(a.length()==1{
					a=" "+a+" ";
				}
				if(a.length()==2{
					a=" "+a;
				}
				System.out.print(a+" ";
			}
			System.out.println();
		}
	}
}

Summary

That's all about the example code for two output results of Pascal's triangle implemented by Java programming in this article. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site. Welcome to leave a message if there is anything insufficient. Thank you for your support to this site!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You may also like