蓝桥杯杨辉三角形java
时间: 2023-11-12 10:56:05 浏览: 156
蓝桥杯 基础练习 杨辉三角形
好的,关于蓝桥杯杨辉三角形的问题,可以使用Java编程语言来实现。下面是一个简单的实现示例:
```java
import java.util.Scanner;
public class PascalTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要打印的行数:");
int rows = scanner.nextInt();
int[][] triangle = new int[rows][];
for (int i = 0; i < rows; i++) {
triangle[i] = new int[i + 1];
triangle[i][0] = 1;
triangle[i][i] = 1;
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
// 打印杨辉三角形
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(triangle[i][j] + " ");
}
System.out.println();
}
}
}
```
阅读全文