Blue Bridge Cup basic exercises BASIC-6 Triangle

Basic training Triangle

1. 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.

We are given below of the first four rows Triangle:

1

1 1

1 2 1

1 3 3 1

Given n, the first n rows outputs it.
Input Format

Input contains 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.
2. The main code is as follows:

import java.util.Scanner;
public class BASIC_6 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[][] array=new int[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<=i;j++){
				if(i==j||i==0||j==0){
					array[i][j]=1;
				}
				else if(i==1){
					array[i][j]=1;
				}
				else{
					array[i][j]=array[i-1][j-1]+array[i-1][j];
				}
				System.out.print(array[i][j]+" ");
			}
			System.out.println();
		}

	}

}

Released nine original articles · won praise 0 · Views 67

Guess you like

Origin blog.csdn.net/LilyS1/article/details/104011660