一维数组输出等腰三角形的杨辉三角Java
时间: 2023-09-29 15:05:01 浏览: 78
数据结构 杨辉三角形
5星 · 资源好评率100%
public class YangHuiTriangle {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = nums.length;
int[][] triangle = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
triangle[i][j] = 1;
} else {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(triangle[i][j] + " ");
}
System.out.println();
}
}
}
阅读全文