杨辉三角java代码,要求是等边三角形,不限制行数

来源:百度知道 编辑:UC知道 时间:2024/06/22 09:12:46

public class YangHuiTriangle {
public static void main(String[] args) {
triangle(5);
}

public static int[][] triangle(int n) {
int[][] yh = new int[n][n];
for (int i = 0; i <= n-1; i++) {
for (int j = n; j >= 0; j--) {
System.out.print(" ");
if (i >= j) {
if (i == 0 || j == 0) {
yh[i][j] = 1;
System.out.print(yh[i][j]);
} else {
yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j];
System.out.print(yh[i][j]);
}
}
}
System.out.println();
}
return yh;
}
}
结果:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

public class YangHui{
public static void main(String[] args){
int n=13;
int[][] triangle=new int[n][];
for(int i=0;i<n;i++){
triangle[i]=new int[i+1];
triangle[i][0]=triangle[i][i]=1;
for(int