1 / * 33 [program 33 Pascal triangle]
2 Title: Print out the Triangle (required to print out 10 lines below)
3 Program Analysis:
. 4 1
. 5 1 1
. 6 1 2 1
. 7 1 3 3 1
. 8 1. 4. 6. 4 1
. 9 1. 5 1. 5 10 10
10 * /
. 11
12 is / * analysis:
13 * ======================
14 * Pascal's triangle features:
15 * 1. each number is equal to the sum of the two numbers above it.
16 symmetrically * 2. Each row of numbers, a 1 is gradually increased.
17 * 3. The number of n-th row has n entries.
18 * =======================
19 === rule of law ================================== ==== ====== law ============================
20 * observed after:
21 is . 1
22 is . 1. 1
23 is . 1. 1 2
24 . 1. 1. 3. 3
25 . 1 . 6. 1. 4. 4
26 is . 1. 5. 1. 5 10 10
27 The first row A1 [0] =. 1
28 second line A2 [0] =. 1, A2 [. 1] =. 1;
29 of the third row a3 [0] = 1, A3 [. 1] A2 = [0] + A2 [. 1], A3 [2] =. 1;
30 of the fourth row a4 [0] = 1, a4 [1] = a3 [0] + a3 [1], a4 [ 2] A3 = [. 1] + A3 [2], A4 [. 3] =. 1;
31 is ....
32 n-th row an [0] = 1, an [1] = a (n-1) [0] + a (n-1) [ 1], an [2] = a (n-1) [1] + a (n-1) [2], an [3] = a (n-1) [2] A + (n--. 1) [. 3] ...
33 is AN ... [-n-2] = A (n--. 1) [n--. 3] A + (. 1-n-) [2-n-], AN [. 1-n-] =. 1;
34 is
35 considered for loop output 10 lines! Also changes the value of a for loop array
36 directly with the output of a two-dimensional array! ! ! ! ! ! !
37 * Rule of ========= ==========
38 * The first number in front of each row ni has spaces, i.e. spaces ni to output a first digital output before
39 * not a digital output, and then outputs a blank
40 *
41 is * /
42 is
43 is Package Homework;
44 is
45 public class _33 {
46 is
47 public static void main (String [] args) {
48 // two 10 declare a line 10 dimensional array
49 int n-= 10 ;
50 int[] [] = A new new int [n-] [n-]; // 10 rows and 10 columns array
51 // first cycle controls the row number
52 is for ( int I = 0; I <n-; I ++ ) {
53 is // the number of columns of the second layer loop control
54 is for ( int J = 0; J <= I; J ++ ) {
55 // first column value of each row is 1, the last one to be output per line (i.e., the number of rows equal columns hours) also. 1
56 is IF (J == 0 || J == I) {
57 is a [I] [J] =. 1 ;
58 }
59 // value of the remaining cases are equal to the number of two and above it
60 the else {
61 is A [I] [J] = A [I -. 1] [J -. 1] + A [I -. 1 ] [J];
62 is }
63 is // System.out.printf ( "% D \ T", A [ I] [J]); // output value
64 }
65 // System.out.println (); // wrap
66 }
67
68 // output result of an isosceles triangle, two for loops to
69 // first one output control line
70 for ( int I = 0; I <n-; I ++ ) {
71 is // first control output portion of the second space layer
72 for ( int J = 0; J <= Ni; J ++ ) {
73 is of System.out.print ( "" );
74 }
75 // second layer of the second array control output value + ""
76 for ( int K = 0; K <= I; K ++ ) {
77 the System.out .print (A [I] [K] + "" );
78 }
79 System.out.println (); // wrap
80 }
81
82 }
83 }