其他分享
首页 > 其他分享> > 打印杨辉三角

打印杨辉三角

作者:互联网

import java.util.Scanner;

public class Class {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("打印杨辉三角形的行数:");
        int row = scanner.nextInt();
        int[][] a = new int[row][];
        for (int i = 0; i < a.length; i++) {
            a[i] = new int[i + 1];
            for (int j = 1; j <= row - i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    a[i][j] = 1;
                } else {
                    a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
                }
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}

标签:Scanner,int,打印,System,print,杨辉三角,new,out
来源: https://www.cnblogs.com/xing-yongjie/p/16226430.html