杨辉三角

杨辉三角

杨辉三角_第1张图片

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入杨辉三角的行数:");
        int n = sc.nextInt();
        int[][] arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            arr[i][0] = 1;
            arr[i][i] = 1;
        }
        for (int i = 2; i < n; i++) {
            for (int j = 1; j < i; j++) {
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                System.out.print("\t");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print(arr[i][k] + "\t\t");
            }
            System.out.println();
        }
    }

你可能感兴趣的:(杨辉三角)