Java数组之杨辉三角

public class ArrayTestor02 {
    public static void main(String[] args) {
        //构造杨辉三角
        int[][] yanghui = new int[10][];
        for (int i = 0; i < yanghui.length; i++) {
            int[] row = new int[i + 1];
            yanghui[i] = row;
            for (int i1 = 0; i1 < row.length; i1++) {
                if(i1 == 0 || i1 == row.length -1){
                    row[i1] = 1;
                }
                else{
                    int[] preRow = yanghui[i -1];
                    row[i1] = preRow[i1] + preRow[i1-1];
                }
            }
        }

        for (int[] ints : yanghui) {

            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}


//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
//1 5 10 10 5 1
//1 6 15 20 15 6 1
//1 7 21 35 35 21 7 1
//1 8 28 56 70 56 28 8 1
//1 9 36 84 126 126 84 36 9 1 
posted @ 2023-05-14 12:57  无风听海  阅读(12)  评论(0编辑  收藏  举报