@java blue bridge group B Problem Fundamentals cup (30) Question 6: Triangle

@java blue bridge group B Problem Fundamentals cup (30) Question 6: Triangle

Keywords: basic training, (two-dimensional) arrays

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

Code:

java.util.Scanner Import;
public class the Main {
public static void main (String [] args) {
Scanner Scanner new new = S (the System.in);
int = n-s.nextInt ();
IF (n-> = n-&&. 1 <= 34 is) {
int [] [] = shuzu2 new new int [n-] [];
for (int A = 0; A <n-; A ++) {// create 2D Pascal triangle
shuzu2 [a] = new int [ n]; // because the length of the array is different in each cycle, so that at every new need
shuzu2 [a] [0] =. 1;
shuzu2 [a] [a] =. 1;
for (int. 1 = B; B <= a ; b ++) {
shuzu2 [a] [b] = shuzu2 [a-1] [b-. 1] + shuzu2 [a-1] [b]; b-th equal to the a-1, line // a-th row b-1 th plus the a-1 line b-th
}
}
for (int A = 0; A <n-; A ++) {// print Pascal's triangle
for (int b = 0; b <= a; b ++) {
of System.out.print (shuzu2 [A] [B] + "");
}
System.out.println (); // every cycle of the output after wrap
}

	}
}

}

Published 29 original articles · won praise 1 · views 1100

Guess you like

Origin blog.csdn.net/DAurora/article/details/104155497