nonsense, direct code, code comments clarify everything ... Two-dimensional array implementation
/** * Yang Hui's triangular two-dimensional array implementation * @author last D * * * Package experiment;
Import Java.util.Scanner;
public class Triangle2d {public static void main (string[] args) {System.out.println ("Enter the specified number of Yang Hui's Triangle lines:");
Scanner sc = new Scanner (system.in);
int n = sc.nextint ();
Sc.close ();
int[][] temp = tri (n);
System.out.println ("Printed Yang Hui's triangle is as follows:");
Print (temp, n); } Static int[][] tri (int n) {int[][] yh = new int[n][]; Assign space to one dimension yh[0] = new INT[1];
Assigning space to two-dimensional yh[0][0] = 1; for (int i = 1; i < n; i++) {Yh[i] = new int[i+1];
Give the two-dimensional allocation space yh[i][0] = yh[i][i] = 1;
for (int j = 1; j < I; J +) {Yh[i][j] = Yh[i-1][j-1] + yh[i-1][j];//value is the value of the previous line diagonally and} return YH;
static void print (int[][] temp, int n) {int col = (2*n-1)/2;
for (int i = 0; i < n; i++) {//Print space for (int k = 0; k < col; k++) {System.out.print ("");
//print numeric for (int j = 0; J <= i; j +) {System.out.print (temp[i][j]+ "");
} System.out.println ("\ t"); col--;
}
}
}
one-dimensional array implementation
/**
* Using one-dimensional array to implement Yang Hui's triangle
* @author Last D * * *
package experiment;
public class Triangle1d {public
static void Main (string[] args) {
int num = 5;//Yang Hui's triangle rows
int[] temp = tri (num) ;
Print (temp, num);
}
Static int[] tri (int num) {
int sum = num * (num + 1)/2;
int[] D1 = new Int[sum];
D1[0] = 1;
for (int i = 1; i < num; i++) {
int loca = i * (i + 1)/2;//The starting position of the current line
D1[loca] = D1[loca + i] = 1;
for (int j = Loca + 1; j < Loca + i + j) {
d1[j] = d1[j-i-1] + d1[j-i];
}
return d1;
}
static void print (int[] temp, int n) {
int col = (2*n-1)/2;
for (int i = 0; i < n; i++) {
//Print space for
(int k = 0; k < col; k++) {
System.out.print ("");
}
//print numeric
int loca = i * (i + 1)/2;//The starting position of the current line for
(int j = Loca; J <= Loca + i; j +) {
System.out.pri NT (temp[j]+ "");
}
System.out.println ("T");
col--
}}
}
more please go to the small stack of elder brother