一、 題目
Pascal Triangle
In mathematics, Pascal's triangle is a triangular array of the binomial coefficients in a triangle. It is named after the French mathematician, Blaise Pascal.
The rows of Pascal's triangle are conventionally enumerated starting with row n = 0 at the top. The entries in each row are numbered from the left beginning with k = 0 and are usually staggered relative to the numbers in the adjacent rows. A simple construction of the triangle proceeds in the following manner. On row 0, write only the number 1. Then, to construct the elements of following rows, add the number directly above and to the left with the number directly above and to the right to find the new value. If either the number to the right or left is not present, substitute a zero in its place. For example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row.
Please print out the Pascal triangle according to the input values of n. Print your triangle with numbers and spaces and put one space between two numbers. Separate different triangles with a single line. When n=0, stop the program. The maximum value of n is 20.
Sample Input:
3
6
0
Sample Output:
1
1 1
1 2 1
1 3 3 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
二、我的程式碼
import java.util.*;
public class PascalTriangle{
public static void main(String [] args)
{
int DegreeNumber[][]=new int[21][];
int DegreeCounter=0;
for (int i=0;i<DegreeNumber.length;i++)
{
DegreeNumber[i]=new int[i+1];
}
DegreeNumber[0][0]=1;
for (int i=1;i<DegreeNumber.length;i++)
{
for (int j=0;j<DegreeNumber[i].length;j++)
{
if (j==0 | j==(DegreeNumber[i].length-1) )
DegreeNumber[i][j]=1;
else
DegreeNumber[i][j]=DegreeNumber[i-1][j]+DegreeNumber[i-1][j-1];
}
}
Scanner scanner=new Scanner(System.in);
int inNum=0;
while(scanner.hasNext())
{
inNum=scanner.nextInt();
if (inNum==0)
break;
else
{
for (int i=0;i<=inNum;i++)
{
for (int j=DegreeNumber[i].length*3;j<DegreeNumber.length*2;j++)
System.out.printf(" ");
for (int j=0;j<DegreeNumber[i].length;j++)
{
System.out.printf("%-6d",DegreeNumber[i][j]);
}
System.out.println("");
}
}
}
System.exit(0);
}
}
三、執行結果

厲害 啊 大師