Pascal's triangle, is the binomial coefficient A triangle geometry.
Pascal's Triangle Overview
Number of lines per endpoint and the end of the ☃ 1
☃ Each number is equal to the sum of two numbers above it
Each digital line symmetrical ☃, is gradually increased by one
Digital ☃ n-th row of n items
Total ☃ first n rows [(1 + n) n] / 2 number
M ☃ number of n-th row and the n-m + 1 is equal to the number, a combination of one of several properties
Total ☃ first n rows [(1 + n) n] / 2 number
☃ formula: C (n-+. 1, I) = C (n-, I) + C (n-,. 1-I)
Using n-tier Java print Pascal's Triangle
An array of printing using the n-layer Pascal's Triangle
public class YangHuiTriangle {
public static void main(String[] args) {
int n = 0;
System.out.print("请输入杨辉三角的层数n: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int arr[][] = new int[n][];
arr[0] = new int[]{1};
arr[1] = new int[] {1,1};
for(int i = 2;i < arr.length;i++) {
arr[i] = new int[i+1];
arr[i][0] = 1;
arr[i][i]=1;
for(int j = 1;j < arr[i].length-1;j++) {
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
for(int i = 0;i < arr.length;i++) {
for(int p = 0;p < arr.length-i-1;p++) {
System.out.print(" ");
}
for(int j = 0;j < arr[i].length;j++) {
System.out.print(String.format("%4d",arr[i][j]));
}
System.out.println();
}
}
}
result:
This blog and CSDN blog (ཌ ་.Asio Jun ་. ད) simultaneous release