Pascal's Triangle _ Blue Bridge Cup

Loop array

/ ** 
Problem Description 
Triangle known as Pascal triangle, its first row i + 1 is (a + b) i-expanding coefficient. 

  
It is an important property: triangles each number equal to its two shoulders numbers together. 

  
Here are the first 4 lines Triangle: 

  
   . 1 

  
  . 1. 1 

  
 . 1 2. 1 

  
. 1. 3. 3. 1 

  
given n, the first n rows outputs it. 

Input format 
input containing a number n. 

Output format 
output of the first n rows Triangle. Each row from the first row are sequentially output number, using one intermediate spaces. Please do not print extra spaces in front. 
Input Sample 
4 
Sample Output 
. 1 
. 1. 1 
. 1 2. 1 
. 1. 3. 3. 1 
data size and Conventions 
1 <= n <= 34. 
 * / 
Package jiChuLianXi; 

Import java.util.Scanner; 

public  class pascalTriangle { 

    public  static  void PasTAngle ( int n){
        int arr[][] = new int[(1+n)*n/2+1][(1+n)*n/2+1];
        arr[1][1] = 1;
        if(n>1){
            for(int i=2; i<=n; i++){
                arr[i][1] = 1;
                for(int j=2; j<i; j++){
                    arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
                }
                arr[i][i] = 1;
            }
        }
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++)
                if(arr[i][j]!=0)
                    System.out.print(arr[i][j]+" ");
            System.out.println();
        }
                    
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.close();
        PasTAngle(n);
    }

}

Guess you like

Origin www.cnblogs.com/LieYanAnYing/p/12183380.html