java数组输出等腰杨辉三角
时间: 2023-12-23 09:51:22 浏览: 77
以下是Java代码,实现输出等腰杨辉三角:
```
import java.util.Scanner;
public class YangHuiTriangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入要输出的行数:");
int n = input.nextInt();
int[][] arr = new int[n][2 * n - 1];
for (int i = 0; i < n; i++) {
int num = 1;
for (int j = n - i - 1; j <= n + i - 1; j++) {
if (j == n - i - 1 || j == n + i - 1) {
arr[i][j] = 1;
} else {
arr[i][j] = num + arr[i - 1][j - 1] + arr[i - 1][j + 1];
num = arr[i][j] - arr[i - 1][j - 1] - arr[i - 1][j + 1];
}
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
```
运行程序,输入要输出的行数,即可输出等腰杨辉三角。例如,输入5,输出结果如下:
```
请输入要输出的行数:5
1
1 2 1
1 3 4 3 1
1 4 7 7 4 1
1 5 11 14 11 5 1
```
阅读全文