杨辉三角

杨辉三角

 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();
        }
    }

posted @ 2020-07-25 22:31  mx丶  阅读(84)  评论(0编辑  收藏  举报
Live2D