蓝桥杯试题 基础练习 BASIC-6 杨辉三角形——冲刺蓝桥杯第五天
程序员文章站
2022-04-01 12:09:34
...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
matrix[i][0] = 1;
matrix[i][i] = 1;
for (int j = 2; j <= i; j++) {
matrix[i][j - 1] = matrix[i - 1][j - 2] + matrix[i - 1][j - 1];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}