A two-dimensional array of printing 10 lines of Pascal's Triangle

prompt:

1. The first line elements 1, the n-th row has n elements;

2. Each element of the first row and the last element is a 1;

3. From the third row, for the non-first element and the last element of the element, i.e.,

 yanghui [i] [j] = yanghui [i-1] [j] + yanghui [i-1] [j-1];

public  class YangHuiTriangle { 

    public  static  void main (String [] args) {
         // 1. Create and initialize an array 
        int [] [] = yanghui new new  int [10 ] []; 
        
        // 2. Assignment to the array 
        for ( int I = 0; I <yanghui.length; I ++ ) { 
            yanghui [I] = new new  int [I +. 1 ]; 
            
            // 2.1 to the end of the first element of each row is not assigned. 1 
            yanghui [I] [0] =. 1 ; 
            yanghui [I ] [I] =. 1 ; 
            
            //2.2 to the first end of the non-assignment elements, j denotes the number of columns 
            for ( int j=1;j<yanghui[i].length-1;j++) {
                yanghui[i][j] = yanghui[i-1][j] + yanghui[i-1][j-1];
            }
            
        }
        
        //遍历数组
        for(int i=0;i<yanghui.length;i++) {
            for(int j=0;j<yanghui[i].length;j++) {
                System.out.print(yanghui[i][j]+" ");
            }
            System.out.println();
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/wsxdev/p/11610718.html